Search Unity

Problem by accessing variable in script in Prefab

Discussion in 'Scripting' started by mooncake888, Mar 30, 2017.

  1. mooncake888

    mooncake888

    Joined:
    Feb 21, 2017
    Posts:
    12
    hello guys, I am making a card game recently. I have problem by acessing variable in script in prefab.I dont know if the way, with which i access the enum in prefab is wrong.Anyone tell me the solutions? thx alot!
    here is my code
    Code (CSharp):
    1. public class HarzardCardClick : MonoBehaviour {
    2.  
    3. private GameController gamecontroller;
    4. public enum State{Green,Yellow,Red};
    5.  
    6.     private State hazardlevel;
    7.  
    8.     public State Hazardlevel {get;set;}
    9.  
    10. void Start(){
    11. hazardlevel = State.Green;
    12. gamecontroller = (GameController)o.GetComponent (typeof (GameController));
    13. }
    14. void OnMouseDown(){
    15.      
    16.             print (hazardlevel);
    17.             if (hazardlevel == State.Green) {  
    18.                 print("it is green");
    19.                 gamecontroller.ChangeLevel()
    20.             }else  if (hazardlevel == State.Yellow) {
    21.                 print("it change!");
    22.             }
    23.          
    24.         }
    25.  
    26.  
    27. public class GameController : MonoBehaviour {
    28.  
    29. public CardStack player;
    30. private HarzardCardClick hcc;
    31. void Start () {      
    32. CardStactView csv=player.GetComponent<CardStactView> ();
    33. hcc=csv.cardPrefab.GetComponent<HarzardCardClick> ();
    34.     }
    35. public void ChangeLevel(){  
    36.         hcc.Hazardlevel = HarzardCardClick.State.Yellow;
    37.  
    38.     }
    39. }
    40.  
     
    Last edited: Mar 30, 2017
  2. mikael_juhala

    mikael_juhala

    Joined:
    Mar 9, 2015
    Posts:
    247
    What exactly is the issue? Do you get an error?

    Anyway, one problem I can see is having two hazard levels, one private and one public. The component itself is modifying the private one, while GameController is modifying the public one. And the public one is never read.
     
  3. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Considering how the enum is defined in the provided script, the problem is probably the enum. My guess is the enum exists in the script provided but not in the script trying to access this script. I'd solve this problem by creating a new namespace, creating the enum in that namespace, and use the "using" keyword to access it.
     
  4. mooncake888

    mooncake888

    Joined:
    Feb 21, 2017
    Posts:
    12
    thank you for the quick answer!
    my Problem is, the value of enum in HarzardCardClick does not changed by GameController.

    the public hazardlevels is automatic generated by setter and getter in Monodevelop Editor.
     
  5. mooncake888

    mooncake888

    Joined:
    Feb 21, 2017
    Posts:
    12
    I am Java developer servel years, but i am a newie in c#, so i dont know how to fix this problem with namespace, can u give me some example code? thx a lot!;)
     
  6. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    nah forget what i said earlier. i glanced the script and assumed some things.

    @mikael_juhala has the right idea. there really is no point to your public variable. A getter/setter looks something like this
    Code (CSharp):
    1. //private variable
    2. private type _varName;
    3.  
    4. //getter
    5. public type varName { get { return _varName; } }
    6. //setter
    7. public type varName{ set { _varName = value; } }
    but if other scripts are going to have that much access to the private variable through the getter and setter anyway, you might as well make the private variable public and use it directly.
     
  7. mooncake888

    mooncake888

    Joined:
    Feb 21, 2017
    Posts:
    12
    I just doubt about the scirpt that i get from prefab is not the correct script..... in my Game , GameObject player has two scirpt---CardStack and CardStackView,and in CardStactView has variable cardPrefab....., i dont know if
    1. CardStactView csv=player.GetComponent<CardStactView> ();
    2. hcc=csv.cardPrefab.GetComponent<HarzardCardClick> ();

    is correct,in order to get the script HarzardCardClick in cardPrefab.
     
  8. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    Your problem is this, I think:
    Code (csharp):
    1. private State hazardlevel;
    2. public State Hazardlevel {get;set;}
    That's not one field with a getter/setter, it's two entirely separate fields! When you define an implicit getter/setter like that, C# creates an invisible backing field - it doesn't look through the class for a similiar-named field and use that one. So you'd want either:
    Code (csharp):
    1. private State hazardlevel;
    2. public State Hazardlevel {
    3.   get { return hazardlevel; }
    4.   set { hazardlevel = value; }
    5. }
    Or:
    Code (csharp):
    1. public State Hazardlevel { get; set; }
    2. // Code within the class uses Hazardlevel, not hazardlevel
    I would go with the latter. If you ever need to do something more than the default behavior later, you can refactor it then, the interface will still be the same.

    Also, personal nitpick - if you're capitalizing it, it should be HazardLevel!
     
  9. mooncake888

    mooncake888

    Joined:
    Feb 21, 2017
    Posts:
    12
    thank u for the suggestion! i have try to write it like that, but nothing is changed.:( i wonder , the Component which i have got, is not the one i want. so that nothing will be changed in mein Prefab.....