- Home /
How to access scripts other than by name.
I'm very new to Unity, and for the most part game creation. Even so, this is such a simple issue I feel kind of dumb for not being able to solve it haha.
I have a stats script attached to each of my game objects that keeps track of things like health, mass, damage, etc. It also holds special functions that are for each instance of that object. Then I have a weapon, say a lazer gun. That lazer needs to access the health variable in that stats script. The problem is, that stats script is a different script in every single object I have. For instance, I have PlayerShipStats, or JunkBlockStats, etc.
I can't use getComponent<PlayerShipStats>
because the lazer also needs to be able to access getComponent<JunkBlockStats>
as well. Or, any other number of objects.
Is there an easy way to do this? If not, should I just make one really long script called "Stats" and condense them all? It seems terribly unorganized... Thank You for the help, or just reading at all really.
Answer by Molix · Oct 05, 2010 at 06:56 PM
A few ways come to mind. You could create a stats class or interface which your individual items inherit from or implement, or you could messages.
(the following is off-the-cuff code for illustration)
e.g. inherit from base class
class StatItem : MonoBehaviour
{
public abstract void AddHealth( int delta );
}
class PlayerShipStats : StatItem
{
int health = 10;
public override void AddHealth( int delta ) { health += delta; }
}
And to use it:
StatItem stat = target.GetComponent<StatItem>();
stat.AddHealth( -10 );
e.g. implement interface
interface IHealth
{
public void AddHealth( int delta );
}
public PlayerShipStats : MonoBehaviour, IHealth
{
public void AddHealth( int delta ) { health += delta; }
}
And to use it:
IHealth stat = target.GetComponent<IHealth>();
stat.AddHealth( -10 );
e.g. SendMessage
void AddHealth( int delta ) { health += delta; }
And to use it:
target.SendMessage( "AddHealth", -10 );
Thanks a lot. To be honest, this level of syntax was a bit over my head. I got a friend to help me understand abstracting though, and thanks to you I have that down pat now. Interfaces though are a bit beyond me, and I'm just now learning of Send$$anonymous$$essage. Going to go through the documentation on Send$$anonymous$$essage right now and learn how to use it. Your answer was very helpful, even if I don't figure the other two out so easily you've fixed my problem and taught me a new important concept. Thanks a bunch!
I'm glad it helped. The Send$$anonymous$$essage way is probably the easiest; another nice thing about it is that any other components can just implement an "AddHealth" function too, and they'll all get called. So for example, an effect script could create some particles or play a sound when the health changes. Anyway, good luck!
Figured out how the other two methods posted work after some research. I learned a lot today! =P Using the Send$$anonymous$$essage method it now works much better than I ever could have hoped. $$anonymous$$ission Complete! <3
Your answer
Follow this Question
Related Questions
How to address scripts without knowing their names? 2 Answers
acces variables from other script 2 Answers
Accessing other gameobject's script variables : why doesn't this work? 2 Answers
When exactly do scripts have to be named exactly the same as the class they contain? 3 Answers
NullReferenceException when using GetComponent to access script variables 1 Answer