Boolean isn't working?

Hi there,

I’m making a pretty simple game where there’s 2 players, each player is prompted to hit a key and if they hit that key before the opposing player they move a gameobject closer to the opposition. When that gameobject hits one of the players it’s game over.

My script is pretty ghetto but I’m pretty new to Unity and time isn’t on my side so I’ve chose to do it this way.

When I hit my allocated keys down the bottom, they move the gameobject perfectly fine but they don’t go back and give me a new random gameobject.

#pragma strict
 
var buttons : GameObject[];
var other : GameObject;
private var randNumber: int;
private var hasPressedButton : boolean = false;
 
 
function Start () {

       hasPressedButton = false;
    for (var i = 0; i < buttons.Length; i++)
       buttons*.SetActive(false);*

randNumber = Random.Range(0,buttons.Length);
}

for (var i = 0; i < buttons.Length; i++)
buttons*.SetActive(false);*

function Update () {

buttons[randNumber].SetActive(true);
if(buttons[0]) {
if(Input.GetKeyDown(“q”) && !hasPressedButton)
{
buttons[0].SetActive(false);
buttons[1].SetActive(false);
buttons[2].SetActive(false);
buttons[3].SetActive(false);
buttons[4].SetActive(false);
other.transform.Translate(Vector3(2,0,0));
hasPressedButton = true;
}
}

buttons[randNumber].SetActive(true);
if(buttons[1]) {
if(Input.GetKeyDown(“w”) && !hasPressedButton)
{
buttons[0].SetActive(false);
buttons[1].SetActive(false);
buttons[2].SetActive(false);
buttons[3].SetActive(false);
buttons[4].SetActive(false);
other.transform.Translate(Vector3(2,0,0));
hasPressedButton = true;
}
}

buttons[randNumber].SetActive(true);
if(buttons[2]) {
if(Input.GetKeyDown(“e”) && !hasPressedButton)
{
buttons[0].SetActive(false);
buttons[1].SetActive(false);
buttons[2].SetActive(false);
buttons[3].SetActive(false);
buttons[4].SetActive(false);
other.transform.Translate(Vector3(2,0,0));
hasPressedButton = true;
}
}

buttons[randNumber].SetActive(true);
if(buttons[3]) {
if(Input.GetKeyDown(“r”) && !hasPressedButton)
{
buttons[0].SetActive(false);
buttons[1].SetActive(false);
buttons[2].SetActive(false);
buttons[3].SetActive(false);
buttons[4].SetActive(false);
other.transform.Translate(Vector3(2,0,0));
hasPressedButton = true;
}
}

buttons[randNumber].SetActive(true);
if(buttons[4]) {
if(Input.GetKeyDown(“t”) && !hasPressedButton)
{
buttons[0].SetActive(false);
buttons[1].SetActive(false);
buttons[2].SetActive(false);
buttons[3].SetActive(false);
buttons[4].SetActive(false);
other.transform.Translate(Vector3(2,0,0));
hasPressedButton = true;
}
}

}
Here’s a screenshot so you can better visualize what’s going on, disregard the dude on the right… I’m still working on that
[12007-screen+shot+2013-06-14+at+3.09.03+am.png|12007]_
_

First off, it looks like you only call the random number once, and that’s during the start. If you do that, it will never be called again, and when you go through the update, it won’t give you any new values. So when they step through the update, you’re going to run once, and have the same number throughout.

When you say GameObject, are you referring to the player (1 or 2) moving? Or are you saying it won’t give you a new button? (That is also a GameObject in your code.)

To me, it looks like only one “other” is made, and this “other” is hard code to move a set distance (2 in the X plane.) This basically means, whichever object is attached, it will move over. Is this some online game or local game? It seems like this code will only make one player move, is that correct?

You could try:

Start()
{
//A bool value to know we have a number set
bool bRandNumberSet = true;
randomNumber = Random.Range(0, buttons.Length);

}

Update() 
{

if(!bRandNumberSet)
{
//When we've used a number, we should enter here to roll again
randomNumber = RandomRange(0, buttons.Length);
bool bRandNumberset = true;
}

//when a user presses the random number or button for assigned random number, we make it false, so next Update call, it will reset to a new number.

//Also a switch case might help clean up a bit

buttons[randomNumber].SetActive(true);
switch(randomNumber)
{

case(0):
// Do something
break;

case(1):
//do something
break;

//... etc
}

I’m not to sure how your input knows which player hits which. I see you’re looking for qwert keys, but is that just Player 1 or 2 or both?

I hope this helps in some way.