Event not being invoked
I followed a Unity Events tutorial and adapted it to my game link: https://youtu.be/38D8AbR8TVU I have 1 script called GameLogicScript, 1 script called UIScript and 1 script for the events called Events
When I invoke an event from the UIScript, the GameLogicScript detects it and does accordingly how it is supposed to do, however, when I try to do it the other way (GameLogicScript invokes and event and UIScript detects it is not working) This is my relevant code:
--------------- UIScript ---------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIScript : MonoBehaviour
{
public GameObject pauseMenuUI;
public GameObject gameOverMenuUI;
public GameObject mainMenuUI;
public GameObject gameplayUI;
//Play - pause button
public Button pauseResumeButton;
public Sprite pauseSprite;
public Sprite playSprite;
//Score
public Text scoreText;
private int score;
void Start()
{
//Listen to events
Events.gameOver.AddListener(GameOver);
Events.increaseScore.AddListener(IncreaseScore);
Events.playAgainUI.AddListener(PlayAgain);
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.Escape)){
ResumeOrPause();
}
}
public void PlayAgain(){
Debug.Log("Holiwis");
pauseMenuUI.SetActive(false);
gameOverMenuUI.SetActive(false);
mainMenuUI.SetActive(false);
gameplayUI.SetActive(true);
}
public void PlayAgainForButton(){
Events.playAgainGameLogic.Invoke();
}
}
--------------- GameLogicScript ---------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameLogicScript : MonoBehaviour
{
//Cameras
public GameObject menuCamera;
public GameObject gameplayCamera;
//Bool
public static bool isPlaying = false;
public static bool playAgain = false;
//Float
private float InstantiateToppingY;
//GameObject
private GameObject NextTopping;
public GameObject NewToppingPrefab;
//Others
public Sprite[] ToppingSprites;
void Start(){
InstantiateToppingY = -2;
//Listen to events
Events.instantiateTopping.AddListener(InstantiateTopping);
Events.resume.AddListener(Resume);
Events.pause.AddListener(Pause);
Events.mainMenu.AddListener(LoadMenu);
Events.startGame.AddListener(StartGame);
Events.playAgainGameLogic.AddListener(PlayAgain);
if(playAgain){
Debug.Log("Invoked event");
Events.playAgainUI.Invoke();
StartGame();
}
else
Pause();
}
public void PlayAgain(){
playAgain = true;
SceneManager.LoadScene("Main");
}
}
--------------- Events ---------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public static class Events
{
public static UnityEvent gameOver = new UnityEvent();
public static UnityEvent instantiateTopping = new UnityEvent();
public static UnityEvent increaseScore = new UnityEvent();
public static UnityEvent resume = new UnityEvent();
public static UnityEvent pause = new UnityEvent();
public static UnityEvent mainMenu = new UnityEvent();
public static UnityEvent startGame = new UnityEvent();
public static UnityEvent playAgainGameLogic = new UnityEvent();
public static UnityEvent playAgainUI = new UnityEvent();
}
Answer by meer59 · Jun 29, 2021 at 07:45 AM
Hi, I had a similar problem, Please change like this and should work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public static class Events
{
public static UnityEngine.Events.UnityEvent gameOver = new UnityEngine.Events.UnityEvent();
public static UnityEngine.Events.UnityEvent instantiateTopping = new UnityEngine.Events.UnityEvent();
public static UnityEngine.Events.UnityEvent increaseScore = new UnityEngine.Events.UnityEvent();
public static UnityEngine.Events.UnityEvent resume = new UnityEngine.Events.UnityEvent();
public static UnityEngine.Events.UnityEvent pause = new UnityEngine.Events.UnityEvent();
public static UnityEngine.Events.UnityEvent mainMenu = new UnityEngine.Events.UnityEvent();
public static UnityEngine.Events.UnityEvent startGame = new UnityEngine.Events.UnityEvent();
public static UnityEngine.Events.UnityEvent playAgainGameLogic = new UnityEngine.Events.UnityEvent();
public static UnityEngine.Events.UnityEvent playAgainUI = new UnityEngine.Events.UnityEvent();
}
Please let me know. You might have to change to this same pattern throughout the app.
Rgds
Answer by luislodosm · Mar 20 at 04:17 PM
Invoking an UnityEvent in Start() could produce a null reference.
public UnityEvent event;
void Start()
{
event.Invoke(); // Problem
}
Instead, wait one frame:
void Start()
{
StartCouroutine(InvokeCoroutine());
}
IEnumerator InvokeCoroutine()
{
yield return null;
event.Invoke();
}
Your answer
Follow this Question
Related Questions
How to pass event info to events? 1 Answer
c# - error CS0103: The name `hit' does not exist in the current context (cardboard switching) 1 Answer
Displaying Text on Touch Event 0 Answers
So i want this problem with ui buttons done 1 Answer
Notify a script that a transform has changed its parent 1 Answer