Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Jejh · May 29, 2017 at 07:00 PM · c#eventfreezeevent-handling

Event - Freezes program when called

Hello, I'm making game where gameMenu Canvas should be displayed when I release Q button. For this I have created MenuToggle class which looks like:

 public class GameManager_MenuToggle : MonoBehaviour
     {
 
         public GameObject menuUI;
         public string menuButton;       
 
         private GameManager_Master gameManagerMaster;        
        
         private void OnEnable()
         {
             SetInitialReferences();
             gameManagerMaster.MenuToggleEvent += MenuToggle;
         }
 
         private void OnDisable()
         {
             gameManagerMaster.MenuToggleEvent -= MenuToggle;
         }
 
         void SetInitialReferences()
         {
             gameManagerMaster = GetComponent<GameManager_Master>();
         }
 
 
         private void Update()
         {
             CheckForMenuToggleRequest();
         }
 
         void CheckForMenuToggleRequest()
         {
             if (Input.GetKeyUp(KeyCode.Q))
             {
                 MenuToggle();
             }
         }
 
         void MenuToggle()
         {
             if (menuUI != null)
             {                
                 menuUI.SetActive(!menuUI.activeSelf);
                 Debug.Log("MenuToggle - change UI State");
                 gameManagerMaster.isMenuOn = !gameManagerMaster.isMenuOn;
                 gameManagerMaster.CallEventMenuToggle();
             }
         }
     }

As you can see I'm using GameManager_Master class which is like (I've cut stuff not relevent to this event):

 public class GameManager_Master : MonoBehaviour 
     {
 
         public delegate void GameManagerEventHandler();
         public event GameManagerEventHandler MenuToggleEvent;
        
      
         public bool isMenuOn;

      
         public void CallEventMenuToggle()
         {
             if (MenuToggleEvent != null)
             {
                 MenuToggleEvent();
             }
         }
 
         private void OnLevelWasLoaded(int level)
         {
             Time.timeScale = 1;
         }
 
     }

What is more when MenuToggleEvent is called my event system should trigger a Pause with script:

 public class GameManager_PauseToggle : MonoBehaviour
     {
         public GameObject playerController;
         private GameManager_Master gameManagerMaster;
         private bool isPaused;
 
         private void OnEnable()
         {
             SetInitialReferences();
             gameManagerMaster.MenuToggleEvent += PauseToggle;
             //gameManagerMaster.SpawnMenuToggleEvent += PauseToggle;
         }
 
         private void OnDisable()
         {
             gameManagerMaster.MenuToggleEvent -= PauseToggle;
             //gameManagerMaster.SpawnMenuToggleEvent -= PauseToggle;
         }
 
         void SetInitialReferences()
         {
             gameManagerMaster = GetComponent<GameManager_Master>();
         }
 
         void PauseToggle()
         {
             if (isPaused)
             {
                 Time.timeScale = 1;
                 isPaused = false;
                 playerController.GetComponent<FirstPersonController>().canRotate = true;
             }
             else
             {
                 Time.timeScale = 0;
                 isPaused = true;
                 playerController.GetComponent<FirstPersonController>().canRotate = false;
             }
         }
         
     }

All my stuff is in one single namespace called Simulator. I am using Unity v 5.6.0f3.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · May 29, 2017 at 07:20 PM

You have an infinite recursion in your "GameManager_MenuToggle" script. You subscribe the method "MenuToggle" to the "MenuToggleEvent" inside the "GameManager_Master" script. However inside your MenuToggle method you call "CallEventMenuToggle" which in turn will call your "MenuToggleEvent" which again will call the MenuToggle method since it's subscribed to the event.

When you design events you should exactly define who is responsible for raising that event to avoid such problems.

Comment
Add comment · Show 1 · 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 Jejh · May 29, 2017 at 07:35 PM 0
Share

@Bunny83 Thank you... I could not see this. I think that I wrote that accidently when VS was trying to give me some help :D

I have one more question as you obviously know more about unity scripting. Will be PauseToggle called when I open my $$anonymous$$enu?

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

7 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

What could be freezing unity? 2 Answers

destroying object and changing variables with collisions in c# 1 Answer

any one now about the touch events of android swipe events on the android 1 Answer


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