- Home /
Basic Unity C# class instances
Well, this is my last question for awhile, and probably the most basic
I've started making static references to my class called "instance." I understand the concept completely in terms of referencing, but I just don't understand one thing:
Pretend I have 2 classes.. 1 is Weapon and 1 is Health.
In order to access one instance I would type Weapon.instance., and vice-versa for the health and I could use the variables throughout both of those scripts.
My main question, however.. (trying to choose my words carefully)
In the Health class, would I have to set a private variable, "Health" equal to instance.health so that I could use a true instance between scripts, or does Health automatically equal instance.Health if called in another script?
I'll try to think up some more elaboration..
I am not 100% sure what you are asking, but it sounds like you are implementing a singleton model. Do either of these help?
http://wiki.unity3d.com/index.php?title=Singleton
http://answers.unity3d.com/questions/291485/setting-up-and-referencing-a-singleton-in-jsus.html
Answer by whydoidoit · Oct 08, 2012 at 07:53 PM
No you don't need an local copy of the instance variable - you can use Health.Instance - be aware that statics are effectively global variables and should be used with caution.