Accessing a different GameObject

So, I am having trouble accessing a GameObject from another GameObject that is not itself :wink: or a child.

For example:

I have a GameObject that I interact with and a script on that object that when the mouse presses changes a property on another GameObject the code looks like this:

function OnMouseDown(){
     showerPull.translate = (0,0,-2);
}

I am in a GameObject named showerStopper and showerPull is a peer.

Thanks in advance!

Regards,

– Clint

What you want to do is add a public variable of type Transform to this script called showerPull, and then drag and drop the showerPull GameObject to this new property in the inspector of showerStopper.

var showerPull : Transform;

function OnMouseDown(){
     showerPull.Translate(0,0,-2);
}

edit: Fixed silly errors :slight_smile:

Excellent! Thanks.

And I can access any GameObject via this method whether it is a peer or not? Or is there something different for a parent and child objects?

Regards,

– Clint

Yes, this is how you access other GameObjects in scripts almost exclusively. The only gotcha is that you can’t assign GameObjects in your scene to properties of prefabs in your project. If you think about it, that makes sense. The prefab in your project is always going to be there, but the thing in your scene is only sometimes there. (Could have a different scene open, etc)

Please note that I changed my initial reply because I had a couple of things wrong. First of all, Translate is a function in the component Transform so you want to refer to the Transform part of the GameObject. You can refer to any component on a GameObject, just do whichever makes sense. Second of all, Translate is capitalized, and that counts. Third of all, Translate is a function, so you don’t want that equals sign in there.

Hope this rambling helps :slight_smile:

I’m sure there are good parts in the manual or tutorials about this you’ll want to check out to become clear on this.

Cheers,
-Jon

Definitely makes sense!

Thanks again for the help.

– Clint

You also might want to be aware of GameObject.Find.

Example usage:

anotherObject = GameObject.Find(“JustAnObject”);
anotherObject.transform.position = transform.position + Vector3(0,3,0);

This finds the object called “JustAnObject” and assigns it to the variable “anotherObject”, then moves it to three units above the GameObject the script is attached to, by adding 3 to the y axis. Note that Find is somewhat processor intensive, so you wouldn’t want to call it every frame inside an Update function. There’s also FindChild if you want to refer to children of the GameObject.

Another option is GameObject.FindWithTag, which, as you would expect, finds an object based on what its tag is (which you assign using the tag manager). Otherwise it works the same way, but doesn’t have the performance cost that Find does.

Using the “var anotherObject : Transform” method + drag’n’drop is what you would normally do, but sometimes you have to use Find, like if you needed to dynamically affect different objects with the same code…you can’t, after all, drag multiple objects onto the same variable.

–Eric

Lihe this one in the scripting overview: Accessing Other Game Objects

Definitely good to know about those other ways to access objects and sort of when you would. That is certainly one of the problems I am running into. I saw those functions but wasn’t sure when the appropriate time is to use what function.

Okay so maybe I don’t understand totally. I have read this a few more times making sure it is clear in my head. The I read this is:

Write the script above and attach it to the showerStopper GameObject in property inspector.

Is that correct?

Regards,

– Clint

Read this one first:
http://unity3d.com/Documentation/Manual/Scripting.html

Then read the one suggested by keli:
http://unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

Got it now! :slight_smile:

Thanks!

– Clint

May I just add: games about bathroom fixtures have been sorely lacking for some time.

:lol:

Yeah, gaming is really going down the toilet…

–Eric