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
2
Question by bushdiver · Mar 02, 2013 at 10:17 AM · javascriptgameobjectreferencestartawake

How to reference GameObject in Awake or Start

When I try to assign a component variable in Start or Awake and then reference it in Update I get an error:

 function Start () { 
     var player = GameObject.FindGameObjectWithTag("Player"); 
     var freeMovementMotor = player.GetComponent(FreeMovementMotor);
 }

 function Update () { 
     if (Input.GetMouseButtonDown (1)) {
     freeMovementMotor.walkingSpeed += 4;  
     }
 }

Error: Unknown identifier: 'freeMovementMotor'

If I declare the component in the Update it works just fine:

 function Update () { 
     var player = GameObject.FindGameObjectWithTag("Player"); 
     var freeMovementMotor = player.GetComponent(FreeMovementMotor);
     if (Input.GetMouseButtonDown (1)) {
     freeMovementMotor.walkingSpeed += 4;  
     }
 }

But I don't want to Find the component every second..

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
2
Best Answer

Answer by bushdiver · Apr 19, 2013 at 03:28 AM

In case anyone else has this problem..

I couldn't get assigning gameobjects by Tag in the Start/Awake function and then referencing it in the Update to work.. ever.
I tried again and again and always received errors when trying to reference the declared and already assigned variable within Update.

The only way to get it to work is to assign the variable again and again on every Update, but that sucks.

Maybe I'm doing something wrong, i don't know, but I'm tired of testing it. Every time I think.. "hmm maybe I was doing something wrong that one time".. "let me do as the docs say, it will be different this time".. "it will work this time"..... NOOOO!

I'm convinced it's a Unity error.
so.. moving on

What I have found to work, so far on every occasion, is referencing the Gameobject in Start via the object's name rather than it's tag .

Using the above code as example:

This Works:

 function Start () { 
     player = GameObject.Find ("Player"); 
     freeMovementMotor = player.GetComponent(FreeMovementMotor);
 }

Instead of this, that Doesn't Work:

 function Start () { 
     player = GameObject.FindGameObjectWithTag("Player"); 
     freeMovementMotor = player.GetComponent(FreeMovementMotor);
 }

  

Note: don't forget to declare it before start:

 var player : GameObject;

Or you can declare it in start, whatever:

 var player : GameObject = GameObject.Find ("Player");

 
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
avatar image
3

Answer by Bunny83 · Mar 02, 2013 at 10:20 AM

You have to declare the variable outsde of Start or Update so it becomes a member variable of your script. You can assign a reference to the variable in Start and use it in Update.

 var player : GameObject;
 var freeMovementMotor : FreeMovementMotor;


 function Start ()
 {
     player = GameObject.FindGameObjectWithTag("Player");
     freeMovementMotor = player.GetComponent(FreeMovementMotor);
 }
  
 function Update ()
 {
     if (Input.GetMouseButtonDown (1))
     {
         freeMovementMotor.walkingSpeed += 4;
     }
 }
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 bushdiver · Mar 03, 2013 at 12:41 AM 0
Share

When I do it the way you suggested I get:

$$anonymous$$ identifier: 'free$$anonymous$$ovement$$anonymous$$otor'

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

10 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

Related Questions

Object reference becomes null between Awake and Start after scene load 2 Answers

If a script is attached to more than one gameObject, will Start() & Awake() run more than once? 2 Answers

Reference variable in other script 1 Answer

Automatic Numbering (JS) 2 Answers

Is there a 'Play On Awake' function for animations? 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