Author Topic: Instances of prefab, with controller attached with onClick notify, has no effect  (Read 2407 times)

seamusc

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile

   I have a hero with multiple instances, I attached a controller script to the prefab, while the keyboard inputs work fine, when I drag the game object with the controller to the onClick notify part of the button, it executes the function and sets the boolean variable just fine.

   the trouble starts when I try to access the boolen in the update function, it always shows false.. when it is set to true by the onclick function.

   Here's my code.


  using Assets.Scripts.Utils;
using Assets.Scripts.Utils.GameEvents;
using UnityEngine;

namespace Assets.Scripts
{
    public class BlockController : MonoBehaviourBase
    {
      public Sprite[] FaceSprites;
        private Block _block;
        private TimeEvent _nextFallStep;
        private float _lastInputTime;

        private bool rotate;

        public float FallStepDelay;
        public float InputDelay;

        public void Start()
        {
            _block = GetComponent<Block>();
            _nextFallStep = new TimeEvent(FallStepDelay);
        }

        // 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;

          }
 
            if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W))
            {
                if (Board.CanRotate(_block))
                    _block.Rotate();
            }

            if (Time.time - _lastInputTime >= InputDelay)
            {
                if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
                {
                    MoveDown();
                    _lastInputTime = Time.time;
                }
                else if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
                {
                    if (Board.CanMoveLeft(_block))
                    {
                        _block.MoveLeft();
                        _lastInputTime = Time.time;
                    }
                }
                else if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
                {
                    if (Board.CanMoveRight(_block))
                    {
                        _block.MoveRight();
                        _lastInputTime = Time.time;
                    }
                }   
            }
        }

        private void MoveDown()
        {
            if (Board.CanMoveDown(_block))
                _block.MoveDown();
            else
                FreeBlock();
        }

        private void FreeBlock()
        {
            Destroy(this);
            Board.Place(_block);
        }
    }
}


I hope I am in the right place, maybe I should have asked on the Unity forums?

Thanks,

Seamus

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
It's a private variable, meaning it's not shared between your scripts. Each script has a local instance of it.

seamusc

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Hello ArenMook,

 Thanks for your reply,

 I made the variable public

public bool rotate;

public void rotateblock() {
         _block = GetComponent<Block>();
         rotate = true;
         Debug.Log ("Button Pressed");
      

      }


private void UpdateInput()
        {


         if (rotate) {
            rotate = false;
            Debug.Log (rotate);
                  }

}


When pushing the button, nothing happens.

Although the update function does execute once and changes the boolean to False when the script starts.. only once though.. not when the button is clicked.

Thanks,

Seamus

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Make it static, not public. And in the future please wrap your code in [code]Your code here[/code] block to make it actually readable.

seamusc

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Hello ArenMook,

  Static worked, thank you so much.. although I don't understand  why static work... probably my lack of knowledge on c#,  I guess I have a lot to learn...

  Thank you for the help!

  I really appreciate it..