So, I am having trouble accessing a GameObject from another GameObject that is not itself 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.
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);
}
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
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.
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.
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.