Search Unity

Question How to activate a gameobject with a trigger?

Discussion in 'Scripting' started by Deleted User, Aug 28, 2021.

  1. Deleted User

    Deleted User

    Guest

    Googmorning,
    I'm trying to create a script that shows me immediately after entering play mode 3 images and that activates other images only if some objects inside the scene are activated for the first time. Unfortunately the code I'm using is not able to activate the images after some gameobjects are activated for the first time, could someone help me figure out what's wrong?
     
    Last edited by a moderator: Jul 25, 2022
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You cannot use
    Invoke
    to start a coroutine - you need to use
    StartCorourine
    instead:
    Code (CSharp):
    1. StartCorourine(WaitBeforeShow2());
     
  3. Deleted User

    Deleted User

    Guest

    I tried but in this way the functions starts together, and WaitBeforeShow2 does not start with the trigger.
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    What do you mean by "trigger" in this context? I don't understand.

    From what your
    Start
    method looks like, do you mean you just want to delay the execution of
    WaitBeforeShow2
    by 2 seconds?
    If so, you can do that by just adding another
    WaitForSeconds
    at the very beginning of it:
    Code (CSharp):
    1. public IEnumerator WaitBeforeShow2()
    2. {
    3.   //Immediately wait for 2 seconds after starting this coroutine.
    4.   yield return new WaitForSeconds(2f);
    5.  
    6.   //Do everything else afterwards.
    7. }
    Or add the delay as a parameter so that you can specify it from when you call it in
    Start
    :
    Code (CSharp):
    1. void Start()
    2. {
    3.   StartCorourine(WaitBeforeShow());
    4.   StartCorourine(WaitBeforeShow2(2f));
    5. }
    6.  
    7. public IEnumerator WaitBeforeShow2(float delay)
    8. {
    9.   //Immediately wait for the delay after starting this coroutine.
    10.   yield return new WaitForSeconds(delay);
    11.  
    12.   //Do everything else afterwards.
    13. }