Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FernandoPereira39144 · May 14, 2020 at 08:53 PM · gameobjectgetcomponentchild objectgetcomponentinchildren

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FernandoPereira39144 · May 14, 2020 at 09:31 PM 0
Share

Thanks a ton! It works perfectly, I'll be sure to use this whenever it comes up in the future.

avatar image Hellium FernandoPereira39144 · May 14, 2020 at 09:42 PM 1
Share

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....)

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

204 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Access Components in Children of GameObject? 1 Answer

Counter for sprite under script from GameObject prefab 0 Answers

error CS0118: `New_Career.Fame' is a `field' but a `type' was expected 1 Answer

GetComponent of ALL clones? 2 Answers

Getting a Script without setting a variable to the script name 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges