- Home /
What's the most efficient way to change script from child object
In the past I have done something that works but have always wondered if there was a more efficient (performance wise) way to do the same thing
gameObject.GetComponentInChildren(typeof(Camera), true).GetComponent<HeadBob>().bobbingSpeed = 0.04f;
gameObject.GetComponentInChildren(typeof(Camera), true).GetComponent<HeadBob>().bobbingAmount = 0.4f;
This is what I used in a small FPS project I've played around with in the past and worked alot on small little improvements to controls and responsiveness, in this case I had a script attached to the camera to make the "head" bob with movement, I made it so the ammount and speed would vary on the players movement state (crouching, walking slower, in the air), so whenever it would change I'd alter the variables in there and then reset them when movement returned to normal, in total I had to do this 8 times (counting enabling and disabling for whenever the player was in the air) for each of the variying states and then returning to normal.
Is there a better way to do this?
Answer by Hellium · May 14, 2020 at 09:17 PM
First of all you could retrieve the component once, in the Start method for instance or with a lazy initialization.
private HeadBob headBob = null;
private HeadBob HeadBob => headBob == null
? (headBob = gameObject.GetComponentInChildren(typeof(Camera), true).GetComponent<HeadBob>())
: headBob;
Then just do HeadBob.bobbingSpeed = 0.04f;
Otherwise, you could have a public
/ [SerializeField] private
reference to the script you'll be able to drag & drop in the inspector, depending on your setup.
Thanks a ton! It works perfectly, I'll be sure to use this whenever it comes up in the future.
Prefer the serialized reference in the inspector over the cache in the start method, over the lazy initialization. The last solution has the small null check overhead (it's not much, but if you can avoid it....)
Answer by FernandoPereira39144 · May 14, 2020 at 09:14 PM
Going back into this code from 6 months ago I actually found something slightly better by creating a Camera object in the script and accessing that:
camera.GetComponent<HeadBob>().bobbingSpeed = 0.04f;
camera.GetComponent<HeadBob>().bobbingAmount = 0.4f;
However this still used GetComponent so I'm not sure about this being much better.