- Home /
show gameobject one time only
i have game object i want to display it only one time if i have score < 2000 game object shown and if score > 2000 game object not shown ,after that when score <2000 again game object not shown again (i assign game object as my image into inspector ) i try
if (Scores < 2000) {
Rock1.gameObject.SetActive (true);
}
if (Scores > 2000) {
Rock1.gameObject.SetActive (false);
}
Answer by Stratosome · Aug 18, 2018 at 06:46 PM
Hello!
Well, in situations like these, when you want something to only happen once, it makes sense to have a way to remember if it has already been shown yet or not. The common way of doing this is to use a boolean.
private bool hasShownAlready = false;
// ...
if (Scores < 2000 && !hasShownAlready ) {
Rock1.gameObject.SetActive (true);
hasShownAlready = true;
}
// If the object is active and the score is >= 2000, hide the object
// (I'd make this >= and an else if)
else if (Scores >= 2000 && Rock1.gameObject.activeSelf == true) {
Rock1.gameObject.SetActive (false);
}
Hopefully that'll work for you.
Answer by dan_wipf · Aug 18, 2018 at 05:57 PM
well you’re gameobject wont be shown because it’s turned off emediatly when you’re over 2000 points. try to set the score when its turned off after 2010 points or what ever you like.
Your answer
![](https://koobas.hobune.stream/wayback/20220612173036im_/https://answers.unity.com/themes/thub/images/avi.jpg)