Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Absolutely impossible to wait in a for loop

Discussion in 'Scripting' started by CodingBruh, May 24, 2016.

  1. CodingBruh

    CodingBruh

    Joined:
    May 11, 2015
    Posts:
    114
    Code (CSharp):
    1. public void IncrementSize()
    2.   {
    3.   PowerBar.transform.localScale += new Vector3(1, .05f, 1);
    4.   }
    5.  
    6. for(float i = 0; i < 1; i += .1f)
    7.   {
    8.   Invoke("IncrementSize", .05f);
    9.   while(IsInvoking("IncrementSize"))
    10.   {
    11.   Debug.Log("Waiting..");
    12.   }
    13.   }

    Code snippets. I want it so the bar increases in size overtime. It does it instantly though. And with this code, the game crashes every single time.
     
  2. CodingBruh

    CodingBruh

    Joined:
    May 11, 2015
    Posts:
    114
    And lol what does this mean?

    NullReferenceException: Object reference not set to an instance of an object
    OnLaneClicked.Start () (at Assets/OnLaneClicked.cs:46)
     
  3. CodingBruh

    CodingBruh

    Joined:
    May 11, 2015
    Posts:
    114
    It was working before. This error makes no sense
     
  4. CodingBruh

    CodingBruh

    Joined:
    May 11, 2015
    Posts:
    114
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class OnLaneClicked : MonoBehaviour {
    6.  
    7.     Ball_Handler Comp;
    8.     Text txt;
    9.     GameObject PowerBar;
    10.     bool Down = false;
    11.  
    12.     void IncrementSize()
    13.     {
    14.         PowerBar.transform.localScale += new Vector3(1, .05f, 1);
    15.     }
    16.  
    17.     void OnMouseDown()
    18.     {
    19.         Down = true;
    20.         if(!Comp.Throwing)
    21.         {
    22.             Comp.Throwing = true;
    23.             var Power = 35000;
    24.             var Curve = 0;
    25.             Vector3 mP = Input.mousePosition;
    26.             mP.z = GameObject.Find("Bowling Ball").transform.position.z - Camera.main.transform.position.z;
    27.             Vector3 Pos = Camera.main.ScreenToWorldPoint(mP);
    28.             Vector3 direction = (Pos - GameObject.Find("Bowling Ball").transform.position).normalized;
    29.             for(float i = 0; i < 1; i += .1f)
    30.             {
    31.                 Invoke("IncrementSize", .05f);
    32.             }
    33.  
    34.             Comp.Throw_Ball(35000,direction,0);
    35.         }
    36.     }
    37.  
    38.     void OnMouseUp()
    39.     {
    40.         Down = false;
    41.     }
    42.  
    43.     void Start()
    44.     {
    45.         Comp = GameObject.Find("Bowling Ball").GetComponent<Ball_Handler>();
    46.         txt = GameObject.Find("InfoText").GetComponent<Text>();
    47.         PowerBar = GameObject.Find("Fill");
    48.     }
    49. }
     
  5. CodingBruh

    CodingBruh

    Joined:
    May 11, 2015
    Posts:
    114
    Found out the error reasoning. The text randomly disappeared.. Don't ask me how THAT happened.

    But anywho, I still need help on the for loop wait thing.

    Sorry for alot of comments btw. ;)

    <EDIT>
    Bugs after bugs after bugs! I made a new text and the canvas doesn't wanna render it XD

    Unity sucks sometimes..
     
  6. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,381
    Kiwasi likes this.
  7. CodingBruh

    CodingBruh

    Joined:
    May 11, 2015
    Posts:
    114
    Thanks. But could you give me an example for this code. I've been doing alot and nothing really is working for me.

    Also, look what I have to deal with...
    https://gyazo.com/3d975a1c50d70699e7920be0c2489eca

    What I'm moving around is the red bar you see not moving or resizing.. Explain this to me please xD
     
  8. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,381
    I did give you an example of code. You should now go check it out and try to apply what they explained in the thread to your scenario.

    I don't know whats going on in your UI issue. Try going through the Unity UI tutorials.
     
  9. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
  10. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    If I get your code well, you want to increase a power bar while the mouse button is down and when the mouse is up, you perform a shoot with the accumulated power.

    It's hard to keep your code simple with Coroutines, even more when your code contains some conditional branching.

    I suggest to use Panda BT for such logic.

    Using this tool, Here is a stripped down version of what you want to do:

    This a BT script implementing the charge-and-shoot logic:
    Code (CSharp):
    1. tree("Root")
    2.     sequence
    3.         IsMouseButtonPressed(0) // Don't go further if the mouse button is not pressed.
    4.         fallback
    5.             while IsMouseButtonPressed(0) // Increase power while mouse button is pressed.
    6.                 IncreasePower
    7.             Shoot // Shoot when the mouse button has been released.
    8.  
    And here is the implementation of the tasks:
    Code (CSharp):
    1. using UnityEngine;
    2. using Panda;
    3.  
    4. public class ShootingPower : MonoBehaviour
    5. {
    6.     public float power = 0.0f;
    7.     public float powerIncreaseRate = 1.0f;
    8.  
    9.     [Task]
    10.     void IncreasePower()
    11.     {
    12.         if (Task.current.isStarting)
    13.             power = 0.0f;
    14.  
    15.         power += powerIncreaseRate * Time.deltaTime;
    16.     }
    17.  
    18.     [Task]
    19.     void Shoot()
    20.     {
    21.         Debug.Log("Shooting power at:" + power);
    22.         Task.current.Succeed();
    23.     }
    24. }
    25.  
    Panda BT is a framework based on Behaviour Tree, which is a nice tool to write logic that runs over several frames.

    More info here:
    http://www.pandabehaviour.com/

    Let me know if you have any question.
     
    Last edited: May 24, 2016
  11. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    that panda behavior looks awful. Shameless plug for a problem that doesnt need it.

    Code (csharp):
    1.  
    2.  
    3. void Update()
    4. {
    5.   if(Input.GetMouseButton(0))
    6.      PowerBar.transform.localScale+=newVector3(1, Time.deltaTime , 1);
    7. }
    8.  
    9.  
    very basic implementation with no clamping, but this is about all you need.
     
    Zenix likes this.
  12. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    @JamesLeeNZ Thanks for the critics.

    What you've proposed is not complete thought.

    I guess this is more what you mean with your solution?
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if(Input.GetButtonDown(0))
    4.             PowerBar.transform.localScale = new Vector3(1.0f, 0.0f, 1.0f);
    5.  
    6.         if (Input.GetMouseButton(0))
    7.             PowerBar.transform.localScale += new Vector3(1.0f, Time.deltaTime, 1.0f);
    8.  
    9.         if( Input.GetButtonUp(0) )
    10.             Comp.Throw_Ball(35000,direction,0);
    11.     }
     
    Last edited: May 24, 2016
  13. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I only provided the basic code, not a complete implementation. Better things to do.
     
    LaneFox and Kiwasi like this.
  14. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    To a man with a hammer... ;)
     
  15. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    The code always starts neat and simple. Then, as you add lines to implement what you want to do, it's likely to grow towards spaghetti code, even faster when what you want to do involves some timing and branching. And when you read it back, it becomes less and less clear what it does, which makes it easier for bugs to creep in.

    With Panda BT, each function focus on one simple task completely decoupled from other functions, which is easier to managed. Then the BT script makes use of these simple tasks to describe the overall logic, which is more readable.

    I am more a man with an electric screwdriver, and this problem looks more like a screw to me.

    I'd agree that using such power tool for a single screw, might be overkill. But at least it demonstrates how the tool works and you might think twice before using your hand screwdriver (or your hammer) next time you need to "nail down" a large number of screws.
     
    Last edited: May 25, 2016
  16. CodingBruh

    CodingBruh

    Joined:
    May 11, 2015
    Posts:
    114
    Well thanks everyone. I don't really want to use the Panda framework as I have no idea what it does yet, and are probably easier ways for something such as this. I'll try again using Update examples. :)
     
  17. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Not for a competent developer, however these are the unity forums, where incompetence is rife (its ok, its only because people are new to development.. not because they're stupid...mostly ;))

    sorry, but...
    Code (csharp):
    1.  
    2. [LIST=1]
    3. [*]tree("Root")
    4. [*]    sequence
    5. [*]        IsMouseButtonPressed(0) // Don't go further if the mouse button is not pressed.
    6. [*]        fallback
    7. [*]           while IsMouseButtonPressed(0) // Increase power while mouse button is pressed.
    8. [*]                IncreasePower
    9. [*]            Shoot // Shoot when the mouse button has been released
    10. [/LIST]
    11.  
    does not look easier to manage, and adds a bunch of overhead mangling your bespoke scripting.
     
    RavenOfCode and Kiwasi like this.
  18. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    Sorry, but competent developers are not proofed against spaghetti code. Unstructured code or even code that does not makes sens is pretty common. Otherwise peer review or code refactoring would not be necessary. And nobody said people on these forums are stupid, or did you?

    Alright, It might looks like overhead-mangled-bespoked-scripts-by-ericbegue™ to you because you are not familiar with it (and I got the feeling you're are not going to be soon :)). You've made your mind about this tool and it seems is definitely not something for you. Though, in my opinion, you should really have a look at Behaviour Tree, not particularly to Panda BT, but at the general AI concept. It's a really good tool to have as a programmer.