Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

invoke couldn't be called

Discussion in 'Editor & General Support' started by theninjafury, May 15, 2020.

  1. theninjafury

    theninjafury

    Joined:
    May 15, 2020
    Posts:
    2
    Hi there. I'm very new to unity and c#, and the forums, so I'm sorry if I've done something wrong ahah.

    Anyway, I've been having issues with using invoke. I'm trying to create a very simple program in which an apple 'ripens' on a tree, and then the player can collect that apple. The apple get ripened in a method by switching the number of apples of the tree to 1. This method is called using the invoke function on awake, after a ten second period. Then, it should again be invoked after the user has collected the apple. This is in a different script to the 'ripening' of the apple.

    However, when this second invoke command is returning the error message:
    "Trying to Invoke method: appleRipe.scriptRipe.AppleRipe couldn't be called."

    Again, I'm really new to this so I'm sorry for my inevitable stupid mistakes. Here is the code for the method which causes the problem:

    Code (CSharp):
    1.  
    2. public void Update()
    3.     {
    4.         if(nearApple == true)
    5.         {
    6.             if(scriptRipe.numberOfApples >= 1)
    7.             {
    8.                 if(Input.GetKeyDown(KeyCode.Z))
    9.                 {
    10.                     scriptRipe.numberOfApples = scriptRipe.numberOfApples - 1;
    11.                     applecollectplayer.playerApples = applecollectplayer.playerApples + 1;
    12.                    
    13.                     scriptRipe.Invoke("scriptRipe.AppleRipe", scriptRipe.ripeTime);
    14.                 }
    15.             }
    16.         }  
    17.     }
    This is the method it's attempting to call:
    Code (CSharp):
    1.    
    2. public void AppleRipe()
    3.     {
    4.         numberOfApples = 1;
    5.     }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,944
    Don't put the name of your variable into the Invoke call. All you need is:
    Code (CSharp):
    1. scriptRipe.Invoke("AppleRipe", scriptRipe.ripeTime);
    This kind of error demonstrates why it is a bad idea to use Invoke in Unity. Coroutines are much more flexible and come with compile-time error checking that would prevent this issue.
     
  3. theninjafury

    theninjafury

    Joined:
    May 15, 2020
    Posts:
    2
    Thanks so much. I'll have a look into coroutines.
     
  4. BjoUnity3d

    BjoUnity3d

    Joined:
    Jul 24, 2012
    Posts:
    62
    Calling Invoke with a string is a mistake but there's nothing wrong with using Invoke if you write it like this:

    Invoke(nameof(YourMethodName), delay);

    I know this is old but this post comes up when searching.
     
    Jemshal and AdemIssadWytopia like this.