- Home /
The Same Declaration in Different Classes?
I wanna breakdown my script into shorter portion, for better readability and organized. Not sure if this would affect the performance.
But I have an instance in the LateUpdate()
function, specifically rightX = Input.GetAxis("RightStickX");
and other similiar instances that I want to use in different classes.
I know that I could declare and initialize the same instance, also I could use a new instance by declaring it from the other class. I have a feeling that they still consume more memory as I make more classes that use the same instance.
My question is, is there a way to use the same object since the float value that I get here is the same all around? Or should I just use Input.GetAxis()
without instantiating it?
P.S. I'm following tutorial, not sure why Input.GetAxis()
is instantiated in the first place. Would love to know if there's any use other than, maybe making it easier to type?
$$anonymous$$aybe I've misused or still don't quite understand the term yet.
To get on the same page as you are, rightX = Input.GetAxis("RightStickX");
Aren't we getting Input.GetAxis()
instantiated with rightX
? How do we define an instantiation?
So the fact that Input.GetAxis is a static function, rightX
still points to the same location. I think this corrected understanding solved my problem simultaneously. Thanks!
No problem. Glad I could help. =)
Would you $$anonymous$$d me converting this exchange to an answer so you could mark it as correct?
Answer by GameVortex · Jan 06, 2014 at 09:30 AM
I am not sure what you mean with Input.GetAxis getting instantiated. Nothing gets instantiated there, it just gets a float value of the specified axis. You can perfectly fine use Input.GetAxis multiple places. Or you can make your rightX value public and reference it in your other scripts.
Instantiation would basically be when we create new objects, like instantiating prefabs or using the New keyword to call constructors. The Input.GetAxis only calls the static function GetAxis in the Input class, which returns a float value. No new objects has been created, we have ins$$anonymous$$d received a value that was stored in the Input class.
Your answer
Follow this Question
Related Questions
OnTriggerEnter() not working 1 Answer
Behind the scenes voodoo w/ Instantiate 2 Answers
Instantiate class from other script but same gameObject 3 Answers
Distribute terrain in zones 3 Answers
C# Utility Class 1 Answer