- Home /
Need help with this please.
So I'm trying to access a variable from one class inside the script of another.
In this particular case I want to get a return of a bool variable named attacking from player
so for example
public player plyr;
plyr = gameObject.getComponent();
if(player.attacking){ ... }
I tried returning the bool variable in a function, but I'm having just as hard of a time getting a class to call another class' functions. So if anyone can tel me how to do this, that'll be awesome. Cause the documentation doesn't seem to help me much in this regard. Thanks!
Answer by kacyesp · Sep 04, 2014 at 06:20 AM
To access instance variables, you have to use an instance of the class, which in this case is referred to by the variable "plyr". You only use the class name to access static variables.
public player plyr;
plyr = gameObject.getComponent<InsertTypeOfObjectHere>();
if(plyr.attacking){ ... }
Sorry, I had gotten this to work shortly after I said it didn't (stupid error on my end) and never updated. Thanks for the answer!
Your answer
Follow this Question
Related Questions
Magic Spell Types - SpellType.Fire/Water/Earth? 1 Answer
Acces variables before start() 1 Answer
NullReferenceException : Object reference not set to an instance of an object 0 Answers
What is the best way to store data about a GameObject? 1 Answer
Can I choose what to pass into .getcomponent<{VARIABLE}>(); 1 Answer