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

Getting parents variable (gameobject)

Discussion in 'Scripting' started by Dmajster98, Aug 21, 2013.

  1. Dmajster98

    Dmajster98

    Joined:
    Aug 10, 2013
    Posts:
    21
    so i have this child object and what i want is to get a gameObject from my Parents script called "neighbours"

    Code (csharp):
    1.     if(transform.parent.GetComponent(Neighbours).RayHitObjectLeft != null)
    2.         {
    3.             var LeftParent : GameObject = transform.parent.GetComponent(Neighbours).RayHitObjectLeft.gameObject;
    4.         }
    this is how i do it but it isn't working :(
     
  2. YodelYum

    YodelYum

    Joined:
    Apr 3, 2013
    Posts:
    44
    Google is, as always, your friend.
    Maybe this will help you

    Code (csharp):
    1. gameObject.transform.parent.gameObject
     
  3. Dmajster98

    Dmajster98

    Joined:
    Aug 10, 2013
    Posts:
    21
    this won't do because the object i want is the gameobject saved under the RayHitObjectLeft variable .
     
  4. YodelYum

    YodelYum

    Joined:
    Apr 3, 2013
    Posts:
    44
    Im not quite sure if i understand your problem fully..
    Wouldn't RayHitObjectLeft.transform.parent work?
    If not pls try to explain your problem a bit more..
     
  5. Dmajster98

    Dmajster98

    Joined:
    Aug 10, 2013
    Posts:
    21
    Hey , yes you don't really understand this , i made a little picture to show you what i mean

    View attachment $Dafunk.bmp

    also what i failed to mention is that the red object has the variable stored in a script called "Neighbours"
     
  6. Dmajster98

    Dmajster98

    Joined:
    Aug 10, 2013
    Posts:
    21
  7. YodelYum

    YodelYum

    Joined:
    Apr 3, 2013
    Posts:
    44
    First of all, this is a forum, and there are not hundreds of people just waiting for u to have a problem, so stop bumping and chill.
    Secondly, by just searching terms like 'unity parent component' or 'unity parent gamobject' etc i found tons of information and similar forum-entries.
    so eg:
    -> http://answers.unity3d.com/questions/158601/get-component-in-parent.html
    -> http://docs.unity3d.com/Documentation/ScriptReference/Transform-parent.html
    -> http://answers.unity3d.com/questions/57055/getcomponent-to-read-up-hierarchy.html

    etc...
     
  8. Dmajster98

    Dmajster98

    Joined:
    Aug 10, 2013
    Posts:
    21
    thanks for helping me with nothing on your 3rd brilliant post ... i already saw all the 3 sites you posted and atleast 10 more , the thing is i did everything the same as on the script with the only difference that it DOESN'T WORK
     
  9. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    I am finding it hard to understand what you want. Do you want to change a var in a script on a child?

    Have you looked into send message?
     
  10. Dmajster98

    Dmajster98

    Joined:
    Aug 10, 2013
    Posts:
    21
    ok i have 2 objects

    1. Parent object with a script called Neighbours
    2. a child object with a script called mesh_deformation

    i need to access object 1's Neigbour script and get a variable out of it called RayHitObjectLeft , this variable has a Gameobject stored in itsellf , the thing is i can't get it to show in my 2nd object

    i use the following code
    Code (csharp):
    1.  
    2. var LeftParent : GameObject = transform.parent.GetComponent(Neighbours).RayHitObjectLeft;
     
  11. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    First of all - post error messages if you get them.
    Second - that statement makes a lot of assumptions about the hierarchy of your GameObject which may or may not be true so consider splitting it up into multiple statements.
    Lastly - that overload of GetComponent returns a Component object not a Neighbours object. Component has no member named RayHitObjectLeft. Cast to Neighbours to access that value.
    Code (csharp):
    1.  
    2. var LeftParent : GameObject = (transform.parent.GetComponent(Neighbours) as Neighbours).RayHitObjectLeft;
    3.  
     
  12. Dmajster98

    Dmajster98

    Joined:
    Aug 10, 2013
    Posts:
    21
    i still don't get the value of RayHit in my 2nd script , i have no errors and my hierarchy is just 2 objects :/
     
  13. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    show us a screen shot of your scene running