- Home /
Usage of Static Monobehaviour
I was wondering if this is the right way to use them. I am having multiple classes which interact with each other
For Example: PlayerItems interacts with Player using the reference itemManager instead of using GetComponent<> i feel this is much easier to handle. Is there any other way to do this? and is the better than SendMessage? or slower than? public class PlayerItems : MonoBehaviour {
public static PlayerItems itemManager;
public GameObject head;
public GameObject body;
void Awake () {
itemManager = this;
}
public void Equip()
{
//Do Something
}
}
Answer by Jamora · Oct 02, 2013 at 12:51 PM
What you (almost) have there is called the Singleton pattern. An alternative to the singleton is the Monostate pattern (apparently there is no wikipedia page). Either is faster than using SendMessage.
Yea well sort of singleton because every scene will have its own version. I'll use singleton with 'DontDestroyOnload(this)' if i wanted only a single version through out the game.
What i was asking is, generally which one is used while development? I've been working on Unity for a while and i hope i am using it right.
You use the same one during development as you do in the released product. Doesn't really matter which one, as long as it works.
Yea i have worked on some released titles :P i wanted to know if there is any better way of doing it.
Anyways Thanks.
Answer by citizen_rafiq · Oct 02, 2013 at 02:16 PM
I always avoid to use static member unless i'm not sure member has Class level access. though static member's are 25% faster access then non static member but static member are occupied memory at first before non-static members and they did not release the memories until the application close. if static member reference to image file's then in future when more memory need application will receive memory warning.
In my test result I saw SendMessage() is slower then using GetComponent().action