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?
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.
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.
Ya cause you are redeclaring the variable, do something like
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.
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
Ya that has to be done in a method, trying doing it in your Start() or Awake() method.
void Start()
{
itemName = "Sword";
}
Put that in your child class.
Works! This is what I searched for. Thank you much!
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.