How do make an object appear and disappear after t seconds?
I want to make an object appear on collision, wait for a couple of seconds and make it disappear again.
public class TriggerObject : MonoBehaviour {
public GameObject object;
void OnTriggerEnter(Collider other)
{
object.SetActive(true);
// Wait for a second
object.SetActive(false);
}
}
I have no idea how to make this to wait for a second... I didn't figure out how to build in "yield WaitForSeconds(1);". Is there any function which can solve my problem or is there any other way to solve this?
Answer by doublemax · Sep 01, 2016 at 06:42 PM
You can only use WaitForSeconds() inside a Coroutine.
void OnTriggerEnter(Collider other)
{
StartCoroutine( ShowAndHide(object, 1.0f) ); // 1 second
}
IEnumerator ShowAndHide( GameObject go, float delay )
{
go.SetActive(true);
yield return new WaitForSeconds(delay);
go.SetActive(false);
}
Really sorry to bring a dead thread to life but is there a way that you can add a part to that script so the object only appears once?
I don't know if you ever figured something out, but maybe you could try to include a counter. This way, you can limit the amount of times the object appears (i.e. counter = 0, object appears, then disappears, counter goes up from 0 to 1. If (counter <= 1), process does not repeat. Just a general idea.
i got a error $$anonymous$$essage When putting it in Here is my Code i attached a screenshot of the errors i'm getting also
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TriggerObject : $$anonymous$$onoBehaviour
public GameObject object;
void OnTriggerEnter(Collider other)
{ StartCoroutine( ShowAndHide(object, 1.0f) ); // 1 second } IEnumerator ShowAndHide( GameObject go, float delay ) { go.SetActive(true); yield return new WaitForSeconds(delay); go.SetActive(false); }
@TheCyborne You need to put “{“ after “$$anonymous$$onoBehaviour” and “}” at the end of the script. Gotta remember those parentheses
@highpockets i got another error After i fixed my last error and forgive me i'm new to coding and Unity Here is my Code
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TriggerObject : $$anonymous$$onoBehaviour {
public GameObject object;
void OnTriggerEnter(Collider other)
{ StartCoroutine( ShowAndHide(object, 1.0f) ); // 1 second } IEnumerator ShowAndHide( GameObject go, float delay ) { go.SetActive(true); yield return new WaitForSeconds(delay); go.SetActive(false);
}
Ok, you said you’re getting an error, but didn’t mention what it said. Hard to help, but I believe it is because you are missing an additional parentheses at the end of the script “}” , you are also posting most of your script as regular text which doesn’t help. Format the code and use the script button
@highpockets Here is my Code Again i'm Sorry about the Question `
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TriggerObject : $$anonymous$$onoBehaviour {
void OnTriggerEnter(Collider other)
{
StartCoroutine( ShowAndHide(Star, 1.0f) ); // 1 second
}
IEnumerator ShowAndHide( GameObject go, float delay )
{
go.SetActive(true);
yield return new WaitForSeconds(delay);
go.SetActive(false);
}
Sorry, but did you read my last comment? You are missing the last parentheses. You need to put this:
}
At the very end of your code
@highpockets thank you for the help Sorry i somehow read over it but thanks for the help
Your answer
Follow this Question
Related Questions
reload script assemblies is VERY Slow. 1 Answer
Noobie question: wait a random time 1 Answer
How to use IEnumerator? Pls help! 1 Answer
Wait in unity(coroutine not working) 0 Answers
How to delay an action in Untiy 1 Answer