Absolutely impossible to wait in a for loop

public void IncrementSize()
  {
  PowerBar.transform.localScale += new Vector3(1, .05f, 1);
  }

for(float i = 0; i < 1; i += .1f)
  {
  Invoke("IncrementSize", .05f);
  while(IsInvoking("IncrementSize"))
  {
  Debug.Log("Waiting..");
  }
  }

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.

And lol what does this mean?

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

It was working before. This error makes no sense

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class OnLaneClicked : MonoBehaviour {

    Ball_Handler Comp;
    Text txt;
    GameObject PowerBar;
    bool Down = false;

    void IncrementSize()
    {
        PowerBar.transform.localScale += new Vector3(1, .05f, 1);
    }

    void OnMouseDown()
    {
        Down = true;
        if(!Comp.Throwing)
        {
            Comp.Throwing = true;
            var Power = 35000;
            var Curve = 0;
            Vector3 mP = Input.mousePosition;
            mP.z = GameObject.Find("Bowling Ball").transform.position.z - Camera.main.transform.position.z;
            Vector3 Pos = Camera.main.ScreenToWorldPoint(mP);
            Vector3 direction = (Pos - GameObject.Find("Bowling Ball").transform.position).normalized;
            for(float i = 0; i < 1; i += .1f)
            {
                Invoke("IncrementSize", .05f);
            }

            Comp.Throw_Ball(35000,direction,0);
        }
    }

    void OnMouseUp()
    {
        Down = false;
    }

    void Start()
    {
        Comp = GameObject.Find("Bowling Ball").GetComponent<Ball_Handler>();
        txt = GameObject.Find("InfoText").GetComponent<Text>();
        PowerBar = GameObject.Find("Fill");
    }
}

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. :wink:

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

Unity sucks sometimesā€¦

If you want to wait a period of time you need to use a Coroutine.

For example,

1 Like

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ā€¦

What Iā€™m moving around is the red bar you see not moving or resizingā€¦ Explain this to me please xD

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.

Coroutines are the right route. Check out the docs, scripting, manual, video.

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:

tree("Root")
    sequence
        IsMouseButtonPressed(0) // Don't go further if the mouse button is not pressed.
        fallback
            while IsMouseButtonPressed(0) // Increase power while mouse button is pressed.
                IncreasePower
            Shoot // Shoot when the mouse button has been released.

And here is the implementation of the tasks:

using UnityEngine;
using Panda;

public class ShootingPower : MonoBehaviour
{
    public float power = 0.0f;
    public float powerIncreaseRate = 1.0f;

    [Task]
    void IncreasePower()
    {
        if (Task.current.isStarting)
            power = 0.0f;

        power += powerIncreaseRate * Time.deltaTime;
    }

    [Task]
    void Shoot()
    {
        Debug.Log("Shooting power at:" + power);
        Task.current.Succeed();
    }
}

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.

that panda behavior looks awful. Shameless plug for a problem that doesnt need it.

void Update()
{
  if(Input.GetMouseButton(0))
     PowerBar.transform.localScale+=newVector3(1, Time.deltaTime , 1);
}

very basic implementation with no clamping, but this is about all you need.

1 Like

@JamesLeeNZ Thanks for the critics.

What youā€™ve proposed is not complete thought.

I guess this is more what you mean with your solution?

    void Update()
    {
        if(Input.GetButtonDown(0))
            PowerBar.transform.localScale = new Vector3(1.0f, 0.0f, 1.0f);

        if (Input.GetMouseButton(0))
            PowerBar.transform.localScale += new Vector3(1.0f, Time.deltaTime, 1.0f);

        if( Input.GetButtonUp(0) )
            Comp.Throw_Ball(35000,direction,0);
    }

I only provided the basic code, not a complete implementation. Better things to do.

2 Likes

To a man with a hammerā€¦ :wink:

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.

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. :slight_smile:

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ā€¦

[LIST=1]
[*]tree("Root")
[*]    sequence
[*]        IsMouseButtonPressed(0) // Don't go further if the mouse button is not pressed.
[*]        fallback
[*]           while IsMouseButtonPressed(0) // Increase power while mouse button is pressed.
[*]                IncreasePower
[*]            Shoot // Shoot when the mouse button has been released
[/LIST]

does not look easier to manage, and adds a bunch of overhead mangling your bespoke scripting.

2 Likes

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.