- Home /
Makes object appear and disappear after a few seconds
It did not work somehow...
public class Coroutine : MonoBehaviour { [SerializeField] private GameObject laser;
// Start is called before the first frame update
void Start()
{
StartCoroutine(ShowAndHide(laser, 3.0f));
}
IEnumerator ShowAndHide(GameObject go, float delay)
{
laser.SetActive(true);
yield return new WaitForSeconds(delay);
laser.SetActive(false);
}
}
Provided you put the laser object in the inspector field for your component Corountine -- did you? -- your code should work fine. (Note that you're passing an unused go
gameObject to the ShowAndHide function, but this wouldn't stop things from working. You should fix that, though, by either using the go
or removing the parameter and na$$anonymous$$g the function ShowAndHideLaser
.) Ensure you've set the laser correctly, and also check if there's any errors in the console.
I did put laser object in the inspector. The laser only gone and not co$$anonymous$$g back. There's no error at all in the console.
Answer by logicandchaos · Feb 19, 2020 at 12:50 PM
I myself don't like coroutines much, I would use Invoke.
void Start()
{
Invoke("Show",1f);
}
Show()
{
laser.SetActive(true);
Invoke("Hide",3f);
}
Hide()
{
laser.SetActive(false);
}
Unity documentation does not agree with you:
For better performance and maintability, use Coroutines ins$$anonymous$$d.
Answer by derClef · Feb 19, 2020 at 02:30 PM
The code-example should work fine. I shouldn't have to mention this, but please make sure, that you actually have a GameObject with the enabled Coroutine script in your scene, where the laser
field is assigned.
If that's not it, there's two issues I can think of, that would make this not work as expected:
The GameObject containing your Coroutine script is inactive. Coroutine can only run on active MonoBehaviours, meaning
gameObject.activeInHierarchy
should return true.You are manipulating Time.timeScale, so your waiting time is distorted in some way or another. In that case you may want to try
WaitForSecondsRealtime
.
I would also advise against naming your class Coroutine, as that name is taken by a class in the UnityEngine
namespace. If this doesn't solve your problem, it would be helpful to know, what works and what doesn't. See if there is anything useful in the console or try using Debug.Log
to see which part of your code executes and which doesn't to narrow down the problem.
Answer by hilfygame-studio · Feb 19, 2020 at 10:48 PM
this works for me just fine:
public GameObject laser;
void Start()
{
StartCoroutine(ShowAndHide(3.0f));
}
IEnumerator ShowAndHide(float delay)
{
laser.SetActive(true);
yield return new WaitForSeconds(delay);
laser.SetActive(false);
}
I'm trying to make the laser come and go repeatedly btw. Not just come and go. The code actually works just fine.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Lerp in Coroutine (Crazy Behavior) 2 Answers
Using coroutines to pause the game 1 Answer
Adding additional Gameobjects to my player Object makes it go crazy? 0 Answers