- Home /
How to appear object on trigger delay?
Hi! I have a question and I hope somebody could help me further. So, I want a object in my game to appear after 7 seconds when my player has entered a specific area. I think it's something with OnTriggerEnter, but how do I put this in my script? At the moment, this is my script.
public class AppearSeconds : MonoBehaviour
{ public GameObject Object;
void Start()
{
StartCoroutine(ShowAndHide(7.0f));
}
IEnumerator ShowAndHide(float delay)
{
Object.SetActive(false);
yield return new WaitForSeconds(delay);
Object.SetActive(true);
}
}
When I start the game, after 7 seconds the object appears, so the delay works. But i want the counter to start running only after the player triggers with another object (area in the game). The area is called 'CubeScene2' and the object that I want do appear after 7 seconds is 'Icoontje'. Right now, the script is connected to 'CubeScene2'. As you can see, there is a public game object in the script. I place the object 'Icoontje' in this public game object. Can somebody help me?
Answer by Beylan0 · Jul 03, 2020 at 01:03 PM
You could check if your object enters a specific trigger by assigning a new layer for that specific object (9 is the index value of your trigger object's layer):
private void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.layer == 9)
{
StartCoroutine(ShowAndHide(7.0f));
}
}
IEnumerator ShowAndHide(float delay)
{
Object.SetActive(false);
yield return new WaitForSeconds(delay);
Object.SetActive(true);
}
Thank you for your answer! It does work now, but not exactly how I want it to. If I start the game and put the player directly in to the trigger area (the transparent cube object, connected with this script) then the object 'Icoontje' appears after 7 seconds, as it should be. But when I don't put the player directly in to the area (the invisible cube is a bit further away), you will see the object 'Icoontje' directly.
I think I need something to do with Object.SetActive false and only set to true when the player collides with the area (transparent cube), but how? Note, I'm a bit new to Unity, therefore the questions haha.
Thank you, thank you, thank you! Legend! It works now :)
Answer by Timvb_97 · Jul 06, 2020 at 12:42 PM
Hi @Beylan0 I have another question, maybe you also know what I need to do. In the same game, you hear a voice over. Right now, the voice overs (different audio sources) are connected to the player. Example, the player walks to an empty bottle and grabs it (VR), then you will hear a voice over "Hmm, empty, I need to refill this". This bottle is connected to a script with 2 public audio sources and a void OnTriggerEnter, as follows public class TriggerSFX : MonoBehaviour { public AudioSource playSound; public AudioSource playSound2;
void OnTriggerEnter(Collider other)
{
playSound.Play();
playSound2.Play();
}
} The audio source with the line "Hmm, empty, I need to refill this" is connected to the player. This audio source will be put in the public audio source playSound. This works, but not as I want it to do. I only want the sound to be heard just 1 time. Right now, you will hear this sound everytime you pick the bottle up. And further, you see 2 audio sources and 2 triggers. This is because in some cases, I want 2 voice overs on one trigger but the second voice over a bit later, couple seconds, with delay. I've searched the whole internet to find the answers for both questions and tried many things, but I just couldn't figure it out. Can you help me with this?
Your answer
Follow this Question
Related Questions
issue with making a moving object trap c# (SOLVED) 2 Answers
How to make object clickable when user enter collider ? 0 Answers
Delay between clicks and sound 2 Answers
Instantiation Wont Stop 1 Answer
Item duration doesn't stack 3 Answers