- Home /
How to remain active on a timed gameobject
Hello im making a simple trivia game and im trying to remain the gameobject active when the correct answer is clicked. The problem is the correct answer is active only for a second then is inactive then the next answer is active. How can I get it to remain active when gameobject is chosen no matter the time limit?
Game Manager Script
public GameObject[] anw;
int index;
private float timeBtwAct;
public float startTimeBtwAct;
public float actWait;
void Start()
{
timeBtwAct = startTimeBtwAct;
for (var i = 0; i < anw.Length; i++)
anw[i].SetActive(false);
}
void Update()
{
if (timeBtwAct <= 0)
{
index = Random.Range(0, anw.Length);
for (var i = 0; i < anw.Length; i++)
anw[i].SetActive(false);
anw[index].SetActive(true);
timeBtwAct = actWait;
}
else
{
timeBtwAct -= Time.deltaTime;
}
}
GameObject script function when clicked
void OnMouseDown()
{
gameObject.SetActive(true);
AudioSource.PlayClipAtPoint(clip, transform.position);
GetComponent<Animator>().enabled = true;
}
Answer by SamKirin · Mar 07, 2019 at 08:46 PM
Game Manager Script
for (var i = 0; i < anw.Length; i++)
if(!anw[i].isClicked)
anw[i].SetActive(false);
anw[index].SetActive(true);
GameObject script function
public bool isClicked = false;
void OnMouseDown()
{
gameObject.SetActive(true);
AudioSource.PlayClipAtPoint(clip, transform.position);
GetComponent<Animator>().enabled = true;
isClicked = true;
}
i understand like this. is it ok?
These are 2 different scripts and the game manager script is not letting me put a bool in the code for (var i = 0; i < anw.Length; i++) if (!anw[i].isClicked) anw[i].SetActive(false);
did you declare a bool as public in gameobject?
i guess variable 'anw' is a gameobject, so i think game manager can access to public variables of gameobject.
Yes anw is a gameobject with its own script and the bool is declared as public but for some reason it comes up with an error I even tried public static bool.
i forgot component. you have to get component with script name before accessing. if your script(and class) name is 'Answer', try like this.
for (var i = 0; i < anw.Length; i++) {
Answer answer = anw[i].GetComponent<Answer>();
if(!answer.isClicked)
anw[i].SetActive(false);
}
anw[index].SetActive(true);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
physics.OverlapSphere colliders 1 Answer
Game optimisation using SetActive partially ineffective. 1 Answer
Script Two Button Functions At Once 1 Answer