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]_
_