- Home /
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..
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");
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;
}
}
When I do it the way you suggested I get:
$$anonymous$$ identifier: 'free$$anonymous$$ovement$$anonymous$$otor'
Your answer
Follow this Question
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