- Home /
gameobject not setting active after timer ends
So I have a healthbar but I also have some ingame instructions. I have set it up so that after the timer ends (when the ingame instructions stop), the healthbar should appear but it is not working. Here is a snippet of my code.
private float Waittime = 49f;
private void Active()
{
if (Waittime == 0)
{
gameObject.SetActive(true);
Debug.Log("jlkj");
}
else
{
Waittime -= Time.deltaTime;
gameObject.SetActive(false);
}
}
` The problem is that the timer isn't ending. I know this because the healthbar is not apearing when the timer is on (which is what I have used in my code). could somebody help me?
Answer by tuinal · Jun 13, 2020 at 03:43 AM
Your problem is due to the fact the scripts attached to a gameobject stop running when the gameobject isn't active.
The simple fix here would be to create a container gameObject, that instead has the script attached, with the gameObject you want to disable assigned to it.
You can alternatively disable components rather than the gameObject itself, e.g. GetComponent().enabled=false. This means scripts keep running, but the individual element turns off, e.g the image is no longer rendered.
A fuller solution would be to use an Animator and cue transitions (i.e. set mechanim state transitions for a UI animation), since for general UI stuff having things immediately 'switch off', rather than smoothly transition, isn't usually a good UX.
Now the health bar doesn't even switch off. I used your first solution and this is my script for the gameobject that contains the healthbar: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Enable : $$anonymous$$onoBehaviour { // Start is called before the first frame update private float Waittime = 49f; public GameObject Game;
private void Active()
{
if (Waittime == 0)
{
Game.SetActive(true);
Debug.Log("jlkj");
}
else
{
Waittime -= Time.deltaTime;
Game.SetActive(false);
}
}
}
There are some possible problems that would cause 'nothing happening' behaviour
$$anonymous$$ake sure you've assigned Game in the inspector
Active() in itself is meaningless, and needs to be called somewhere, e.g. you need in Update() to call Active().
If waittime is 49f you need to wait 49 seconds (a long time for testing!) for the ==0 condition to be true
Furthermore it's bad practice to expect a float to be equal. Every frame 0.0215351 etc. is getting deducted from 49. It's unlikely it will ever == 0 exactly; use < 0 ins$$anonymous$$d.
nvm that, I tried your first solution (use animator) and it worked but now my health bar doesn't go down when I take damage. Here is my health bar script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Healthbar : $$anonymous$$onoBehaviour
{
public Slider slider;
private float Waittime = 49f;
public Animator anim;
public void Set$$anonymous$$axHealth(int health)
{
slider.maxValue = health;
slider.value = health;
}
public void SetHealth(int health)
{
slider.value = health;
}
}
In the animator, I made the animation 49 seconds long and the health bar appears at the end of the instructions. Now, it doesn't reduce when I take damage.
here is a picture of the animator if it helps:
Ok I fixed it by disabling the animator after the animation ends but since I got the idea from you, ill mark you as the correct answer.
Answer by Unity-Devloper · Jun 13, 2020 at 06:28 AM
I´m not very sure what you mean but I´ll try to answer your question.
When You Power Is Active Then Start Coroutine Or Call Coroutine
StartCoroutine(Active(Waittime));
public IEnumerator Active(float countdownValue)
{
Debug.Log("countdownValue: " + countdownValue);
Game.SetActive(false);
Debug.Log("jlkj");
while (currCountdownValue > 0)
{
yield return new WaitForSeconds(1.0f);
currCountdownValue--;
Debug.Log("countdownValue: " + countdownValue);
}
if (currCountdownValue <= 0)
{
Game.SetActive(true);
Debug.Log("jlkj");
StopCoroutine(Active); // stop it. Note:Stop StopCoroutine is needed if you again start then call as your waiting time
}
}
**Your Refrence If Not UnderStood
https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html https://docs.unity3d.com/ScriptReference/MonoBehaviour.StopCoroutine.html
c# #ui #unity #2d #setactivetime #StartCoroutine #StopCoroutine