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 /
  • Help Room /
avatar image
0
Question by srodrigodev · May 27, 2020 at 06:14 PM · events

Script variables being reset after calling UnityEvent

On a two players game, I have a GameMaster object that keeps track of the player turn, which is an enum.

Depending on the current turn, the units of the player are selected or not when clicked. This triggers is a UnityEvent handled by the GameMaster (probably a bad name BTW, should be Board).

The problem I'm having is that the current turn has a default value (first player) when the UnityEvent triggers. Every time. It doesn't make any sense.

 // GameMaster.cs
 
     using UnityEngine;
     
     public class GameMaster : MonoBehaviour
     {
         private PlayerTurn currentPlayerTurn;
     
         void Start()
         {
             currentPlayerTurn = PlayerTurn.PlayerOne;
         }
     
         void Update()
         {
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 if (currentPlayerTurn == PlayerTurn.PlayerOne)
                 {
                     currentPlayerTurn = PlayerTurn.PlayerTwo;
                 }
                 else
                 {
                     currentPlayerTurn = PlayerTurn.PlayerOne;
                 }
                 Debug.Log("New player turn: " + currentPlayerTurn);
             }
         }
     
         public void onUnitClicked(Unit unit)
         {
             HandleUnitClicked(unit);
         }
     
         private void HandleUnitClicked(Unit unit)
         {
             Debug.Log("player turn: " + unit.PlayerTurn);
             Debug.Log("current turn: " + currentPlayerTurn);
             if (unit.PlayerTurn != currentPlayerTurn)
             {
                 return;
             }
     
             // Here do the stuff for the selected unit
         }
     }
 
 // Unit.cs
 
 using UnityEngine;
 using UnityEngine.Events;
 
 [System.Serializable]
 public class UnitClickedEvent : UnityEvent<Unit>
 {
 }
 
 [System.Serializable]
 public enum PlayerTurn
 {
     PlayerOne, PlayerTwo
 }
 
 public class Unit : MonoBehaviour
 {
     [SerializeField] private PlayerTurn playerTurn; // This is set to the correct value on the editor
     [SerializeField] private UnitClickedEvent onClick;
 
     public PlayerTurn PlayerTurn { get => playerTurn; }
 
     void OnMouseDown()
     {
         onClick.Invoke(this);
     }
 }
 
 The execution when clicking on a player two unit BEFORE switching turn prints:
 player turn: PlayerTwo
 current turn: PlayerOne
 
 This is correct, as player one has de first turn
 
 When switching turn by hitting the space bar, it prints:
 New player turn: PlayerTwo
 
 And when clicking on a player two unit again, it prints:
 player turn: PlayerTwo
 current turn: PlayerOne  // -> This is incorrect!
 
 So, basically, the currentTurn variable is always PlayerOne (default value) inside the event handler despite being set to the correct value


Just to clarify, if I print the value of currentTurn inside the Update method, it always has the correct value driven by the input (spacebar).

I had already this problem with another variable a few days ago. I removed that variable and actually improved the design, but not I'm having the same problem again.

Any ideas why this could be happening? It seems to me like anything invoked from UnityEvent is having the default variables values.

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
0

Answer by srodrigodev · May 30, 2020 at 02:50 PM

Still haven't managed to solve this or understand why it's happening.

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

203 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

Related Questions

[Solved] Scroll not working when elements inside have click events 3 Answers

Can you create a custom 'OnEvent()' function? 1 Answer

Help with events. 1 Answer

Analytics CustomEvent sent without internet connection 0 Answers

Execution order of custom events 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