Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Leroterr · May 22, 2014 at 03:05 PM · updategetcomponentcomponentbooleanstart

GetComponent in the Update function?

Can anyone tell me why transform.parent.GetComponent().follow = followBool; doesn't work in the Start function, but it does on the Update function?

Also, I heard some people saying that putting GetComponent in the Update function is "awful" or something, and that I should avoid it. But for some reason this is the only way I can get my thing to work.

Here's the code for 2 of the scripts with further explanation:

-----Script rocketTrigger-----

 using UnityEngine;
 using System.Collections;
  
 public class rocketTrigger : MonoBehaviour {
  
 public bool followBool;
  
     /*
     The 'follow' bool in rocketFollow actually gets changed to 'true'
     when I set this in the Update function, but it doesn't in the Start function
     People say that Getting Component in the Update is "awful"
     Any better way I should this?
     */
     void Update()
     {
        transform.parent.GetComponent<rocketFollow>().follow = followBool;
     }
  
     void OnTriggerEnter(Collider col)
     {
        if(col.gameObject.CompareTag("plane"))
        {
          //Sets 'followBool' to true, which SHOULD set the 'follow' bool in rocketFollow to true as well...
          followBool = true;
        }
     }
 }

-----Script rocketFollow----- (Parent of rocketTrigger)

 using UnityEngine;
 using System.Collections;
 
 public class rocketFollow : MonoBehaviour {
 
 public bool follow;
 
     void Update () {
 
         if(follow)
         {
             //This gets called when I put the GetComponent line in the Update function, but doesn't in the Start function
             Debug.Log ("Following");
         }
     }
 }

Thanks for the help :)

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

1 Reply

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

Answer by MakeCodeNow · May 22, 2014 at 03:35 PM

You're probably having your issue because there is no parent set when Start is called, which is why you have to put the code in update. The main thing people are telling you to avoid is calling GetComponent every frame if you don't have to. Here's a simple way around that (assuming the parent doesn't change during the lifetime of this object):

 public class rocketTrigger : MonoBehavior {
     private rocketFollow parentFollow;
 
     public bool followBool;
 
     void Update() {
         if(!parentFollow && transform.parent) {
             parentFollow = transform.parent.GetComponent<rocketFollow>();
         }
         if(parentFollow) {
             parentFollow.follow = followBool;
         }
     }
 }
Comment
Add comment · Show 1 · 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 Leroterr · May 22, 2014 at 03:37 PM 0
Share

Oh okay, thanks! Got it to work now.

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

22 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

Related Questions

How do I fix my game after API failing to update my scripts 2 Answers

Script initialization when instancing a game object 0 Answers

How to make this code fire projectile once during the Update function? I know Update means every frame. 1 Answer

Many ways to access other components got me very confused. 1 Answer

how to disable/enable a script in c#? 2 Answers


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