- Home /
Use variables from parent class without needing to re-define them in child class
I have a class which has references to game objects, scripts, etc. I also have a class which inherits from this class. When I try to use variables from the parent class in the child class I get an error because I haven't defined them again in the child class. Is there a way to get around this?
Post your scripts and we can tell you exactly what you are doing wrong.
if that error is occuring is maybe because is marked as abstract?
Answer by Bunny83 · Jun 14, 2019 at 02:28 PM
How did you define your variables in your patent class? Keep in mind that private variables are only visible to the class itself. They exist in derived classes but are not accessible.
In order to have access to variables defined in the parent class from a derived class they need to be either protected or public.
protected variables are only visible to the class itself or from inside any derived class. From outside the class they are not visible just like private variables. Public variables are generally accessible even from outside the class
Like the comments already mentioned we can't tell you what you should do or what exactly you did wrong without seeing the code or without knowing the exact compiler error you get.
Your answer
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Overriding a subtype's variable with its own subtype 1 Answer
Inheritance with Guns 0 Answers
Interaction script 2 Answers