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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to access inherited variable in children class

Discussion in 'Scripting' started by TripleDouble, Apr 7, 2015.

  1. TripleDouble

    TripleDouble

    Joined:
    Mar 18, 2014
    Posts:
    11
    Hello,
    In my class item i have variable
    public string itemName;
    How can I get this variable in it's children class sword and change it? In Unity editor i have object with class sword and there is column for value change of variable, but how can I do this in script?
     
    Last edited: Apr 7, 2015
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    Same way you access any other variable, just with its name, just as if you actually defined it in the child class.

    Just keep in mind that the child class can't access private variables of it'd parent, it can only access public and protected.

    Just be sure not to hide it by defining a new variable with the same name in the child class.
     
  3. TripleDouble

    TripleDouble

    Joined:
    Mar 18, 2014
    Posts:
    11
    Thanks for reply, but I may wrote it bad.
    In Item class i have variable "public string itemName;"
    But in children class sword i want to do something like "public string itemName = "Sword";"
    Because in other class i have condition
    if (thisHuman.itemHave.itemName == "Sword")

    It works correctly, when i assign value in Unity Editor, where is column for assignment but i want do this in a script.
     
  4. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    Ya cause you are redeclaring the variable, do something like
    Code (CSharp):
    1. itemName = "Sword";
    If you use the Public String bit you are redeclaring a new variable that only exists in the child class. Vs just setting a new value on the old variable.
     
  5. TripleDouble

    TripleDouble

    Joined:
    Mar 18, 2014
    Posts:
    11
    I did it like this, but it gave me just error "Unexpected symbol `=' in class, struct, or interface member declaration"
    Even if I write type string before it it doesn't work
     
  6. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    Ya that has to be done in a method, trying doing it in your Start() or Awake() method.

    Code (CSharp):
    1. void Start()
    2. {
    3.     itemName = "Sword";
    4. }
    Put that in your child class.
     
    pewpewpewpewpew and TripleDouble like this.
  7. TripleDouble

    TripleDouble

    Joined:
    Mar 18, 2014
    Posts:
    11
    Works! This is what I searched for. Thank you much!
     
  8. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    You might also want to make it a protected variable, that way it can be accessed by anything that inherits from the class, but can't be set via the inspector or via external scripts.
     
    whoopdeedoo03 likes this.