Search code examples
c#staticunity-game-engineinstancengui

In Unity and NGUI, a prefab, onClick notify works on public function but the boolean loses it's value in update


I am trying to add an NGUI button, that rotates instances of a game object using a controller script that works fine when pressing the arrow keys. I started using NGUI to add buttons. The buttons have a notify on Click that selects a function from the game object's controller script.

I added a rotate function that changes a boolean for the script, the boolean changes when the button is clicked, but when the value is accessed from the update function it's value is not correct.

I assumed it had something to do with the instances of the gameobject, or I am not retrieving the game object in question. So I tried both of those and the Boolean still doesn't update correctly.

Here is the code for my controller:

private bool rotate;

// This is the function in the notify part of onClick in the NGUI button.
    public void rotateHero () {

         rotate = true;  // This works it sets it to true.

    } 


public void Update()
    {
        UpdateInput();

        //if (_nextFallStep.PopIsOccurred() && Time.time - _lastInputTime >= InputDelay)
     if (_nextFallStep.PopIsOccurred())
        {
            MoveDown();
            _lastInputTime = Time.time;
        }
    }


private void UpdateInput()
    {

        if (rotate) {

         if (Board.CanRotate(_block))
                _block.Rotate();

          debug.log(rotate);  //Always returns false.. when it should be true.
          rotate = false;

      }

Solution

  • The private variable needed to be static.

    static bool rotate;