- Home /
How to set gameobject inactive if random.range decides to choose the same gameobject
Hello I’m trying to make a simple trivia game a little more difficult for me to make then I expected lol but I have gameobjects that are set active randomly then back to inactive but I would like for it to go inactive then back to active if unity decides to choose the same gameobject currently it just remains active then go inactive I have a game manager script on a empty gameobject controlling all of the gameobjects.
GM
public GameObject[] anw;
int index;
private float timeBtwAct;
public float startTimeBtwAct, actWait;
void Update()
{
if (timeBtwAct <= 0)
{
index = Random.Range(0, anw.Length);
for (var i = 0; i < anw.Length; i++)
{
AnswersScript answers = anw[i].GetComponent<AnswersScript>();
if (!answers.click)
anw.SetActive(false);
}
anw[index].SetActive(true);
timeBtwAct = actWait;
}
else
{
timeBtwAct -= Time.deltaTime;
}
}
Answer by Moutik · Mar 09, 2019 at 11:55 AM
Not sure I understand but if you want your gameobject to be inactive for just a period of time, you should use a Coroutine to make it go active/inactive after X seconds or any event that you want.
If I'm not mistaken, you cannot use coroutine on an object to disable it and then re-enable after some time, since from the moment it was set inactive, the script would be inactive as well. You'd have to call the coroutine on another object to control that.
Answer by Defetysta · Mar 09, 2019 at 02:24 PM
I think that this may be what you are looking for
anw[index].SetActive(!anw[index].activeSelf);
This line will toggle their state, so would take care of both activating and deactivating.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Scripts still operate even when not active... 1 Answer
Making a bubble level (not a game but work tool) 1 Answer
GameObject.Find() work on inactive objects 16 Answers