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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Invoke to function

Discussion in 'Scripting' started by danielzivharel, Apr 17, 2020.

  1. danielzivharel

    danielzivharel

    Joined:
    Apr 17, 2020
    Posts:
    12
    Hello
    I want to Invoke to function but its not working
    what I need to do that the invoke will call function Restart.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3.  
    4. public class gameMangaer : MonoBehaviour
    5. {
    6.     bool gameHasEnded = false;
    7.  
    8.     public float delayEnd = 1f;
    9.  
    10.     public void endGame()
    11.     {
    12.         if(gameHasEnded == false)
    13.         {
    14.             gameHasEnded = true;
    15.             Invoke("Restart", delayEnd);
    16.         }
    17.      
    18.         void Restart()
    19.         {
    20.             SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    21.         }
    22.     }
    23. }
    24.  
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    Restart is defined inside endGame... it needs to be a member of gameManager.
     
    danielzivharel likes this.
  3. danielzivharel

    danielzivharel

    Joined:
    Apr 17, 2020
    Posts:
    12
    Thank you
    It's working