- Home /
Question by
Henry454545 · Apr 09, 2017 at 05:32 AM ·
unity 5uiarraysactivate
How to Activate UI in an Array Using C#
I am making this game where you have 30 seconds to do something. If you do not do the required task it will display a game over menu (not another scene) but it will Display the menu as soon as the game starts, instead of displaying it after the timer is up.
Here Is My Script:
public float Timer = 30f;
private Text timerText;
public GameObject[] componentsToActivate;
void Start()
{
Timer = 30;
timerText = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
Timer -= Time.deltaTime;
timerText.text = Timer.ToString("f0");
print(Timer);
if (Timer >= 0)
{
activateComponents();
}
}
void activateComponents()
{
GameObject[] allObjects = UnityEngine.GameObject.FindGameObjectsWithTag("GameOverMenu");
foreach (GameObject Obj in componenetsToDisable)
{
Obj.SetActive(true);
}
}
}
Comment
Answer by UnityCoach · Apr 09, 2017 at 07:52 AM
Line 25, you probably meant if (Timer <= 0)
instead of if (Timer >= 0)
.
I would add enabled = false;
so that the component doesn't keep on calling activateComponents() every frame after.