Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Get Parent Game Object C#

Discussion in 'Scripting' started by mercuryplutus, Aug 7, 2010.

  1. mercuryplutus

    mercuryplutus

    Joined:
    Jun 27, 2010
    Posts:
    158
    Been looking through the forums and the scripting manual but cant figure this out

    I have a house game object and i have a letterbox game object attached under it in the scene hierarchy.

    How do I get the house from the letterbox script?
     
  2. cj-currie

    cj-currie

    Joined:
    Nov 27, 2008
    Posts:
    337
  3. mercuryplutus

    mercuryplutus

    Joined:
    Jun 27, 2010
    Posts:
    158
    Hi cjcurrie,

    Thanks, solved with your help. Thought transform would only get a transform object but it has a GetComponent method as well.


    Rob
     
    Deleted User likes this.
  4. nfrrtycmplx

    nfrrtycmplx

    Joined:
    Mar 5, 2013
    Posts:
    1
    Code (CSharp):
    1. public Vector3 GetGuiPosition(float yOffset = 0.01f)
    2.     {
    3.         Vector3 v;
    4.  
    5.         v = Camera.main.WorldToScreenPoint(gameObject.transform.root.position);
    6.         v.x /= Screen.width;
    7.         v.y /= Screen.height;
    8.         v.y += yOffset;
    9.    
    10.         return v;
    11.     }

    This is an old thread - But i was trying so hard to understand GUIText and getting a parented guitext to show up correctly. with cj.currie's help I put together a function that will correct the guitext parented to a single object.

    posting code in case someone else stumbles across the same headache
     
    Deleted User likes this.