Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Unit326 · Mar 18, 2014 at 04:26 PM · parentchildaccessvarable

Calling parent variables from child and visa versa

Hi i would like to know, how does a child call functions or variables from his/her parent? //currently my parent is a Sphere with a green light, and the child is a Sphere with a blue light (both is prefabs). And how does a parent call functions and variables from there children. (i want to do this later)

Ok this is what i have so far. [parent script: SpawnObjectLerp]

//global vars

public GameObject[] SpawnedControls;

public GameObject backPrefab;

public bool moveBack=false;

public int deviceCounter = 0;

private void Ams() {

     SpawnedControls[deviceCounter] = (GameObject)Instantiate(backPrefab, transform.position, Quaternion.identity);
     deviceCounter++;
         
     for (int i=0;i<deviceCounter;i++)
     {
         Debug.Log("Adding gameobject to Parent");
         SpawnedControls[i].transform.parent = transform.parent;    
     }

 }

[The child prefab has a script attached to it with]

//globals

  public GameObject pA;
     
     public void OnMouseDown()
         {
             //string parentname = transform.parent.gameObject.name;
             //bool test;
             //pA = GameObject.Find(parentname);
             //pA = GameObject.Find(transform.parent.gameObject);
             //test= transform.parent.GetComponent<SpawnObjectLerp> ().moveBackFlag;
             //pA.GetComponent<SpawnObjectLerp> ().moveBackFlag = true;
             //pA.moveBackFlag = true;
             //Debug.Log ("die kind" + parentName);
         }

I mostly get the error message "Object reference not set to an instance of an object" I tried allot of different ways but I cant get past this part. All i want is access to the parent class script, to reach the moveBackFlag, and edit it. What i can do is create a empty game object and attached a moveVarsAround sqript to it. Then use this script to move var's back and forward. But i cant imagine this is the way to do it.

Hope someone can show me the right way. Thank you very much in advance.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by MousePods · Mar 18, 2014 at 04:40 PM

First, the variable/method must be public

To get the a variable or call a method from the parent:

 Script parentScript = this.transform.parent.GetComponent<Script>();
 
 script.parentMethod();
 
 or script.parentVariable


There are multiple ways to get the children's scripts. It depends on how many children you have.

 // Get the first child in the children array 
 // use this if you only have one child as you won't 
 // know the order of the children's array)
 this.transform.GetChild(0).GetComponent<Script>();
 
 OR
 
 // If you have multiple children
 this.transform.FindChild("NameOfChild").GetComponent<Script>();

Hope this helps!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Unit326 · Mar 19, 2014 at 07:56 AM 0
Share

Thank you very much, it worked!!! And now i understand it a bit better :)

avatar image Unit326 · Mar 19, 2014 at 07:59 AM 0
Share

I also fixed the line where i assign objects to there parent to:

 SpawnedControls[i].transform.parent=transform;

And my on$$anonymous$$ouseDown now looks like this.

 public void On$$anonymous$$ouseDown()
     {
         Debug.Log ("$$anonymous$$y parent is:" + transform.parent.name);
         SpawnObjectLerp parentScript = this.transform.parent.GetComponent<SpawnObjectLerp>();
         parentScript.moveBackFlag = true;
     }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

Access a child from the parent or other gameObject. 2 Answers

Accessing a variable from a child object and pass it to the parent 2 Answers

Access parents other child from other other child 1 Answer

Trouble accessing child 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges