• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by runetimon · Nov 19, 2011 at 08:36 PM · c#loopforeasy

For loop not working

I got a little bit further with scripting and decided to make a small rpg, now im trying to just set up a script to cast Fire ball. i'm making a fault somewhere, just can't quite seem to find where.

 using UnityEngine;
 using System.Collections;
 
 public class Fireball : MonoBehaviour {

 public int manaCost = 30;
 private string spellName = "Fire Ball";
 public float castTime = 35.5f;
     
 private float curCastTime = 0f;
 private bool spellCasting = false;
 
 
 // Use t$$anonymous$$s for initialization
 void Start () 
 {
 
 }
 
 // Update is called once per frame
 void Update () 
 {
     if(Input.GetKeyDown(KeyCode.G))
     {
         for(float i = 0; i < castTime; i += Time.deltaTime)
         {    
             Debug.Log("---Working---");
             GUI.Box(new Rect(Screen.width / 2 - 50, Screen.height * 0.9f, castTime / curCastTime, 50), curCastTime + "/" + castTime);
             i += Time.deltaTime;
         }    
     }
 }
 
 void OnGUI()
 {
     
 }

}

Btw, t$$anonymous$$s is just to make the loading barr work, you know, for charging spells, i know the GUI.box is in the wrong place, but it doesn't even show my Debug.Log, so im trying to figure out why it's not going through the loop

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Nov 19, 2011 at 08:50 PM

I've not found the reason why Debug.Log isn't printing anyt$$anonymous$$ng, but the whole t$$anonymous$$ng is wrong: you can't block Unity with a lengthy for like t$$anonymous$$s (not to mention the GUI.Box inside Update) - not$$anonymous$$ng will happen w$$anonymous$$le t$$anonymous$$s for is counting time. You could somet$$anonymous$$ng like t$$anonymous$$s instead:

float timeBox = 0;

void Update () { if(Input.GetKeyDown(KeyCode.G)) // if g pressed... { timeBox = castTime; // start the timer } if (timeBox >= 0) // decrement timer w$$anonymous$$le its >= 0 { timeBox -= Time.deltaTime; } }

void OnGUI() { if (timeBox > 0) // display the box w$$anonymous$$le timeBox > 0 { GUI.Box(new Rect(Screen.width / 2 - 50, Screen.height * 0.9f, castTime / curCastTime, 50), curCastTime + "/" + castTime); } }

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image runetimon · Nov 19, 2011 at 10:50 PM 0
Share

Thank you a lot, i made the mistake of not attaching it to an object, so the script wouldn't be excecuted. and its true, when i did attach it, my game just crashed for a few seconds and than the bar was suddenly there, i thank you a lot for this script, i can continue now

avatar image FLASHDENMARK · Nov 19, 2011 at 10:51 PM 0
Share

Remember to "accept" the answer before you leave.

avatar image runetimon · Nov 19, 2011 at 11:03 PM 0
Share

How excactly do i do that :S, im sorry im fairly new here

avatar image aldonaletto · Nov 20, 2011 at 03:47 AM 0
Share

Click the "check" button below the voting thumbs in the answer. It's important to accept the answer: it helps other people to find solution for similar problems.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

For loop C#? 2 Answers

Using for as while. 3 Answers

C# List For Loop Only Returns Last Element 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges