Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Pass the location of an object's parent?

Discussion in 'Getting Started' started by MikeTeavee, Jun 17, 2016.

  1. MikeTeavee

    MikeTeavee

    Joined:
    May 22, 2015
    Posts:
    194
    I want to pass the transform.position and transform.rotation of a GameObject's parent. The function that takes the parameters currently accepts a GameObject, but I don't know the right code to put in the function call.

    Alternatively, Is there a way if I pass the parameter as a Transform? (I have tried this but cannot get it to work).

    Any ideas would be great, thanks!
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    Sure, you can pass a Transform, or a GameObject, or a Vector3. Do whatever's convenient. If all are equally convenient, then I generally recommend doing whatever's the lowest-level... so prefer a Transform over a GameObject, and prefer a Vector3 over either one of those. That gives you the most flexibility — for example, you could pass in a computed position instead of always having to use the position of some game object.

    But anyway.

    If the method you're calling needs a GameObject, and you want to pass in the parent, then simply pass in transform.parent.gameObject.

    If you want to change the method to take a position and rotation (i.e. a Vector3 and a Quaternion), then instead pass in transform.parent.position, transform.parent.rotation.

    If still stuck, please post the code so we can see what you're doing.
     
    MikeTeavee likes this.
  3. MikeTeavee

    MikeTeavee

    Joined:
    May 22, 2015
    Posts:
    194
    This answers both of my questions, thanks!
     
    JoeStrout likes this.