Inherit from specific classes
Hi there everyone, here is what I am trying to do:
I would like to creat a class that inherits all variables from a script that is active in the scene.
for example I have a class called Menu:
public class Menu : MonoBehaviour{
public int i;
void Update(){
if(Input.GetKeyUp(KeyCode.Space)){
i = 5;
}
}
}
And I have a MenuOptions class:
public class MenuOptions: Menu {
void Update(){
print(i);
}
}
Now I want the MenuOptions to print 5 but I have no idea how I could achieve this... Any help is appreciated!
Thanks not what inheritance does. It seems like you just want one script to know about another. There are lots of old answered Qs here that should help explain that. Titles like "Find another Script" or "One script use variables from another."
Well that will work if you make it static: public static int i;
but I don't think it is the best solution.
For something like this you probably want to set it with GetComponent or Send$$anonymous$$essage when the value needs to change.
Thanx for the replies, I wonder if this way of inheriting will be any good but I believe it's pretty useless since I found out I didn't want to use any variables from the $$anonymous$$enu class and since it might be more handy to use GetComponent.
Why static? $$anonymous$$enuOptions is a subclass of $$anonymous$$enu.
Not really sure what the OP wants, but all that seems to be missing are the "virtual" and "override" keywords and a call to base.Update(), and an instance of $$anonymous$$enuOptions would print 5 when space is pressed.
I don't know to be honest. I just know that it works if you change it to static. As I said though... there are better solutions.
Your answer
Follow this Question
Related Questions
Calling Function from another script not working 1 Answer
Disable a group of scripts based on base class. 0 Answers
Can't figure this out! C# access another class 2 Answers
Multiple PowerUps (One ItemBox) 0 Answers
Class value as list of the same class 0 Answers