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. Dismiss Notice

Variable losing values between method calls

Discussion in 'Scripting' started by dipndawtz, Mar 29, 2017.

  1. dipndawtz

    dipndawtz

    Joined:
    Nov 29, 2016
    Posts:
    7
    Hello, I've got a class called ShopManager which handles the logic behind a "shop" in my game.

    I've got a private variable in ShopManager called _selectedAssassin. I set it in a HandleClick method (which gets called from an event trigger on some UI elements).

    However when I try to access that variable from another method shortly after it doesn't exist, and the variable (_selectedAssassin) seems to be null.

    Here are the two relevant code blocks:

    Code (CSharp):
    1.  
    2.        private Assassin _selectedAssassin;
    3.        public void AddAssassinToGuild()
    4.         {
    5.             Debug.Log("Selected assassin from add assassin: " + _selectedAssassin);
    6.  
    7.             var pm = _gameManager.PlayerMoney;
    8.             var hc = _selectedAssassin.HireCost;
    9.  
    10.             if (pm < hc) return;
    11.             _gameManager.PlayerMoney -= _selectedAssassin.HireCost;
    12.  
    13.             _gameManager.AddAssassin(_selectedAssassin);
    14.         }
    15.  
    16.         public void HandleClick(HireCard card)
    17.         {
    18.             _selectedAssassin = card.Assassin;
    19.  
    20.             Debug.Log("Selected assassin from handle click: " + _selectedAssassin);
    21.  
    22.             if (_selectedCardGameObj != null)
    23.                 _selectedCardGameObj.GetComponent<Image>().sprite = DefaultSprite;
    24.  
    25.             _selectedCardGameObj = card.gameObject;
    26.  
    27.             _selectedCardGameObj.GetComponent<Image>().sprite = SelectedSprite;
    28.  
    29.             Debug.Log("Hire cost: " + card.Assassin.HireCost);
    30.         }
    The output I get from the log messages is correct for the handle click one:
    But the one from add assassin is null:
    What am I doing wrong here? I've been fighting with this for the last few hours and I can't seem to figure it out.

    I'm still fairly new to Unity (and the forums) so if I missed any necessary information let me know and I'll add it!
     
  2. kubajs

    kubajs

    Joined:
    Apr 18, 2015
    Posts:
    57
    It seems weird to me. In case you set the _selectedAssasin to some value and really call the AddAssassinToGuild method after the _selectedAssassin is set, the value should be stilll there. Are you sure you don't set it to null anywhere?
    I'd recommend you to trace the code frame by frame in Unity (stop after every frame) so console is not flooded with the debug info or better debug it in Visual Studio.
    Isn't the card.Assassin set to null later anywhere?
     
  3. dipndawtz

    dipndawtz

    Joined:
    Nov 29, 2016
    Posts:
    7
    Thanks for the insight Kubajs, I'll be sure to do that tonight when I get a chance to work on this issue again.

    The card.Assassin comes from the HireCard which is just being pased in by the event system, through the "HandleClick" method. I'm wondering if this value is passed in by reference, and so is being unset when the HandleClick method is done?
     
  4. kubajs

    kubajs

    Joined:
    Apr 18, 2015
    Posts:
    57
    If HireCard is class (I believe so), then card is instance of class so it must be passed by reference. Only value types (struct, int, float and other basic types except of string) can be passed by value. Instance of class are passed by reference anyway. The card instance shouldn't be nulled by default. Once you debug it you'll discover the issue.
     
  5. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Is ShopManager a MonoBehaviour? Is there more than one instance of ShopManager? It's entirely possible that _selectedAssassin is being set (through HandleClick) on one instance of ShopManager but AddAssassinToGuild is being called on an entirely different instance.
     
  6. dipndawtz

    dipndawtz

    Joined:
    Nov 29, 2016
    Posts:
    7
    It seems to be something with the way the event system handles the function call.

    I was able to get around this by adding a "selected" value to the HireCard script, and whenever it gets selected in the HandleClick function setting the selected value (and setting the old selected card to false).