Search Unity

Problem with setting a Property on a MonoBehavior script

Discussion in 'Editor & General Support' started by AdamWaters, Jul 31, 2013.

  1. AdamWaters

    AdamWaters

    Joined:
    Aug 30, 2011
    Posts:
    94
    So I've been struggling with this issue for several days now and cannot figure out why it isn't working.

    Issue:
    The player in my game can create a Building, and each building has a script that controls it's stats. Think tower defense where each building has defenses and attacks. Part of that attached script is an int _PlayerID. When a building is destroyed or created the PlayerID is set so that the game can decide who it belongs too.

    The biggest problem is that this works on most of the buildings. Only a few are not working properly. The _PlayerID always defaults to 0 on these problem buildings. I have gone over the Prefabs for the problem buildings many times and they are exactly the same as the working buildings.

    The script that controls this is pretty simple
    Code (csharp):
    1.  
    2. public int _PlayerID;
    3.  
    4.         void Start ()
    5.     {  
    6.         SetPlayer(_PlayerID);
    7.     }
    8.    
    9.     public void SetPlayer(int playerID)
    10.     {
    11.         _PlayerID = playerID;
    12.        
    13.         if(playerID != 0)
    14.             _Stats.Player = CommonFunctions.GetPlayerByID(playerID);
    15.        
    16.         Debug.Log("PlayerID passed in: " + playerID.ToString());
    17.         Debug.Log("PlayerID property: " + _PlayerID.ToString());
    18.        
    19.     }
    20.  

    Is there any reason why certain Prefabs wouldn't be updating?

    Is it possible that one prefab is cached in some way that is preventing it from updated correctly?

    I have tried changing the PlayerID between public and private (it should be private).

    I have tried calling UnityEditor.SetDirty, but I am pretty sure that is not the issue here since I am just changing the value of an int on a script.

    I have tried marking the int PlayerID as Serialized and still got the same issue.


    Any help or guidance on this issue is greatly appreciated. Thank you!