Search Unity

Accessing a different GameObject

Discussion in 'Scripting' started by littlelingo, Aug 4, 2006.

  1. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    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:

    Code (csharp):
    1.  
    2. function OnMouseDown(){
    3.      showerPull.translate = (0,0,-2);
    4. }
    5.  
    I am in a GameObject named showerStopper and showerPull is a peer.

    Thanks in advance!

    Regards,

    -- Clint
     
  2. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    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.

    Code (csharp):
    1.  
    2. var showerPull : Transform;
    3.  
    4. function OnMouseDown(){
    5.      showerPull.Translate(0,0,-2);
    6. }
    7.  
    edit: Fixed silly errors :)
     
  3. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    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
     
  4. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    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.

    Cheers,
    -Jon
     
  5. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    Definitely makes sense!

    Thanks again for the help.

    -- Clint
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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
     
  7. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Lihe this one in the scripting overview: Accessing Other Game Objects
     
  8. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    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
     
  9. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
  10. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    Got it now! :)

    Thanks!

    -- Clint
     
  11. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    May I just add: games about bathroom fixtures have been sorely lacking for some time.
     
  12. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yeah, gaming is really going down the toilet....

    --Eric