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 /
avatar image
0
Question by eaturbrainz · Dec 18, 2010 at 10:53 PM · javascriptparenting

How can I get a child object to read a variable held by the parent?

Just as a test, I have set up two cubes. One that when clicked, turns red and sets a private variable "isActive" to false, and if clicked again, turns green and sets "isActive" to true.

Code for parent object:

private var isActive = false;

function OnMouseUp () { if (isActive == false) { isActive = true; } else { isActive = false; } }

function Update () { if (isActive == false) { renderer.material.color = Color.red; } else { renderer.material.color = Color.green; } }

The other cube is supposed to turn yellow when hovered over by the mouse only if the "isActive" variable of the parent is true.

Here's the code for the child object:

function OnMouseOver () { //"parentObject" is another game object that actually holds the "isActive" variable. if (parentObject.isActive == true) renderer.material.color = Color.yellow; }

function OnMouseExit () { renderer.material.color = Color.white; }

I know the 4th line of the child's code is wrong, i just put it there to show you what I'm going for. Is there a command or line of code that can read the variable from its parent without having to specifically define the name of the parent object?

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

2 Replies

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

Answer by JoshOClock · Dec 18, 2010 at 11:07 PM

This is the variable you want:

http://unity3d.com/support/documentation/ScriptReference/Transform-parent.html

You should just be able to change your 'parentObject' to 'parent' and use GetComponent() on that to get your script and then your variable.

So something like:

   var parentScript : ScriptName = parent.GetComponent(ScriptName);
    if (parentScript.isActive)
         renderer.material.color = Color.yellow;

Comment
Add comment · 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
0
Best Answer

Answer by Eric5h5 · Dec 18, 2010 at 11:00 PM

if (transform.parent.GetComponent.<ParentScript>().isActive)

Substitute the actual name of the script that's on the parent. Also note that isActive can't be private if you want other scripts to access it. It has to be public, but if you don't want it to show up in the inspector, use @HideInInspector.

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

How to set parents of all objects in an array? 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

I am having an issue with the rotation of a child 1 Answer

Scripted tank track bones behave strange 0 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