- Home /
Proper Syntax with vars and GameObject.Find
hi folks, trying to understand the syntax here in Unity when you want it to search for a GameObject matching a variable. if i define my vars like thus:
var targetobject : GameObject; var message : String;
function OnTriggerEnter() { GameObject.Find(targetobject).SendMessage(message); } i'm trying to make generic the code so that it finds the value of whatever GameObject i select and then sends the message i want (which is a function name in that object), but i would like to define these variables independently in the Inspector if possible. I know i can enter in quotes for each of these as specific Objects and messages but that defeats the purpose.
any and all help appreciated.
Answer by Kryptos · Jan 09, 2012 at 11:53 PM
Firstly, GameObject.Find() takes a string as parameter and not a GameObject reference. So the correct syntax is:
var targetObject : GameObject;
var message : String;
function OnTriggerEnter()
{
var found : GameObject = GameObject.Find( targetObject .name );
found.SendMessage( message );
}
Secondly, if you already have a reference to the GameObject then you don't need to use GameObject.Find(). Thus:
var targetObject : GameObject;
var message : String;
function OnTriggerEnter()
{
targetObject.SendMessage( message );
}
Or you can use a GameObject name, i.e.:
var targetObjectName : String;
var message : String;
function OnTriggerEnter()
{
var found : GameObject = GameObject.Find( targetObjectName );
found.SendMessage( message );
}
But note that the found object will be the first object matching the given name (ordered by instanceID). So be careful if you have more than one object with the same name.
thanks everyone! so the biggest error besides my poor code formatting was that the type attribute was wrong (string vs object name). i had started with Will G's Unity tutorial where he sent a message to another object using the specific Find option and i tried hacking that not knowing i was taking a wrong turn.
anyway thanks again for everyone's help!
Answer by Eric5h5 · Jan 09, 2012 at 11:45 PM
The point of Find is that it takes a string and finds a GameObject of that name. See the docs for the syntax for Find, and note that it specifies that it takes a string and returns a GameObject.
Answer by aldonaletto · Jan 09, 2012 at 11:51 PM
There are several things apparently misunderstood here:
1- GameObject.Find finds objects by name, thus it requires a String argument;
2- SendMessage actually calls a function in the referenced object, and the argument is a String defining the function name - a second argument may be passed, and it will become the called function's argument;
That's an example of how it works:
var onOff = false;
function Update(){ if (Input.GetKeyDown("space")){ // if space bar pressed... onOff = !onOff; // toggle onOff... // and call TurnLight(onOff) in RoomLight object: GameObject.Find("RoomLight").SendMessage("TurnLight", onOff); } } When the space bar is pressed, the object named "RoomLight" is found, and the function TurnLight(onOff) is called - obviously, the "RoomLight" object must have such function:
function TurnLight(state: boolean){ light.enabled = state; }NOTE: All Find functions are too slow to be called frequently: usually they are called only at Start, and the game object found is stored in a member variable to be used when needed without wasting time.