Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by unity_Vz5vZanGJqp62A · Feb 28, 2020 at 12:34 PM · scripting problemeventeventsystemscript error

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();
 
 }
 





Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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();
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

296 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to pass event info to events? 1 Answer

How to use the GPGS ISavedGameMetadata? 1 Answer

"Can't add script behaviour AssemblyInfo.cs" 0 Answers

Unity 5 checking if player isGrounded 1 Answer

I can't build my project, how can I fix it? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges