- Home /
Issue with random gameobject enable / disable script.
I have 5 GameObjects in total (more specifically planes) I want to be able to disable 4 Objects with 1 still displaying, randomly.. so it can be a different GameObject (out of the 5) each time.
The script is incomplete as of yet but it keeps on telling me I'm having an issue on lines 10 - 18, more specifically just after the functionStart.
halp :3
var LeftButton1 : GameObject;
var LeftButton2 : GameObject;
var leftButton3 : GameObject;
var leftButton4 : GameObject;
var leftButton5 : GameObject;
var randNumber: int;
function Start () {
var leftButton1.active = false;
var leftButton2.active = false;
var leftButton3.active = false;
var leftButton4.active = false;
var leftButton5.active = false;
}
function Update () {
randNumber = Random.Range(1,5);
if(randNumber == 1){
var leftButton1.active = true;
var leftButton2.active = false;
var leftButton3.active = false;
var leftButton4.active = false;
var leftButton5.active = false;
if(else){
var leftButton1.active = false;
var leftButton2.active = false;
var leftButton3.active = false;
var leftButton4.active = false;
var leftButton5.active = false;
}
}
}
why would be the script if i don't want to be randomly selected? could u guys help me...
Answer by robertbu · Jun 09, 2013 at 04:39 PM
This task is a good one to use an array. After you've declared an array, you can select the game object with this script attached in the Hierarchy, click on the triangle next to "Buttons," set the size to the number of buttons, and drag and drop the buttons onto the entries in the array. Also if you are using Unity 4x, you want to use GameObject.SetActive(). Also your code sets the active state every frame. Not sure what you really want. This code set it when the 'A' key is pressed:
#pragma strict
var buttons : GameObject[];
private var randNumber: int;
function Start () {
for (var i = 0; i < buttons.Length; i++)
buttons[i].SetActive(false);
}
function Update () {
if (Input.GetKeyDown(KeyCode.A)) {
randNumber = Random.Range(0,buttons.Length);
for (var i = 0; i < buttons.Length; i++)
buttons[i].SetActive(false);
buttons[randNumber].SetActive(true);
}
}
Hey Robert, thank you so much!
..Sorry for the lat reply.. I'm in Tokyo AT$$anonymous$$ and internet has been an issue but anyway..
Thank you so much for your answer, but if I was to tweak the script to make the buttons appear every 3-5 seconds ins$$anonymous$$d of appearing if you press the A button, how would I do that?
Would it be something using?:
yield WaitForSeconds(3)
;randompick();
}
I basically want a new button to appear every 3-5 seconds and if that player hits the button correctly he progresses.
Thanks again Robert.
You cannot use 'yield' inside of Update(). Here is a bit of rewrite that uses InvokeRepeating() to change the button every three seconds:
#pragma strict
var buttons : GameObject[];
private var randNumber: int;
function Start () {
for (var i = 0; i < buttons.Length; i++)
buttons[i].SetActive(false);
InvokeRepeating("SwitchButtons", 0.0, 3.0);
}
function SwitchButtons() {
for (var i = 0; i < buttons.Length; i++)
buttons[i].SetActive(false);
randNumber = Random.Range(0,buttons.Length);
buttons[randNumber].SetActive(true);
}
Ahhhh!!!!
Thank you so so much Robert, this works perfectly!
If you question is answered, click the checkmark next to the answer to close it out. Thanks.
Hey Robert
Been playing around with the script for awhile now, just trying to figure out how I would do a few things:
The Gameobjects to appear randomly
When you press a key the gameobjects would disable for 5 second and then another random gameobject would appear
I want this to pretty much just loop until a player wins.
There's a screenshot of my game so you can better understand what i'm trying to do.
When the random gameobject comes up you're prompted to hit a key, when you hit the point in the middle will move.
Thanks again Robert :3 if you end up reading this
Your answer
Follow this Question
Related Questions
C# GameObjects Instantiate Into Each Other Issue 1 Answer
Three Spots For Three Random Objects 1 Answer
GameObject variable change for all of the objects 1 Answer
How to randomly spawn three non repeating gameobjects from an array? 2 Answers
How to make a gameobject move randomly withing a boundary ? 3 Answers