Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Why isnt this working?

Discussion in 'Scripting' started by warrenbrandt, Apr 15, 2023.

  1. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    hi guys why is the first Invoke underlined in red?

    Code (CSharp):
    1. void StartGame()
    2.     {
    3.         string text = "Welcome to the forest...";
    4.         Invoke("ShowInfoBox", 2.0f,text);
    5.     }
    6.  
    7.     public void ShowInfoBox(string text)
    8.     {
    9.         infoText.text = text;
    10.         Invoke("KillInfoBox", 5);
    11.     }
    12.  
    13.     public void KillInfoBox()
    14.     {
    15.         infoBox.SetActive(false);
    16.     }
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    Because if you look at the docs you'll find there is you'll find
    Invoke
    only accepts to parameters: https://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html

    You can't pass these methods parameters, which is one major restriction of using very old methods such as Invoke.

    If you want to delay the display of something, probably better to use a coroutine.
     
    warrenbrandt likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,758
  4. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    Cool to know, thanks!!!!
     
    Kurt-Dekker likes this.