4
« on: May 30, 2014, 11:36:09 AM »
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