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 SergeantBiscuits · Jul 22, 2012 at 08:40 AM · variableparentchildread

How to read variable from parent object?

Sorry if this is an exceedingly simple question; somehow haven't been able to find an answer.

I have a parent object, "TankChassis", with the script "Movement" (javascript) that contains a variable "directionAngle" that describes the angle it is facing (y component.) The tank chassis is driven around with WASD.

Within TankChassis I have "TankTurret", with the script "TurretControl" that rotates the turret between -110 and +110 degrees (eulerAngles.y) depending on the x position of the mouse.

What I want to do is add directionAngle from TankChassis to the angle of the turret so that the turret rotates in relation to the tank chassis.

What's the best way to do this? Tl;dnr - how do I get a child's script to read a variable from a script in its parent?

(I'm learning Unity by brute force; hope I'm not bothering anyone with simple questions!)

Thank you so much!

(A picture of my cute, derpy tank for your trouble: http://i46.tinypic.com/fnten7.png )

Comment
Add comment · Show 3
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 whydoidoit · Jul 22, 2012 at 09:02 AM 0
Share

Surely if the chassis rotates and it is the parent then the turret rotates with it?

avatar image SergeantBiscuits · Jul 22, 2012 at 11:05 AM 0
Share

Yes, but I want the turret to be able to rotate independently from the tank by mouse control.

avatar image Seth-Bergman · Jul 22, 2012 at 11:27 AM 0
Share

added an edit with a better solution for that see my answer :)

2 Replies

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

Answer by Seth-Bergman · Jul 22, 2012 at 09:22 AM

Edit: moved from comments, really more of an answer i guess

must be taking direct input off of the mouse position, in which case.. if you really want to, you could add a var and pass the difference of the tanks rotation to it..

 offset = currentTankRotation - originalTankRotation ;
 turret.rotation = mousePositionX + offset; //pseudocode

oh,and as for how to read the var,you could just say:

     var tank : Transform;
     tank = transform.parent;
 
    var  tankYRotation = tank.eulerAngles.y; //for example

once you have that, you can access other scripts you're after too, with

 var myScript = tank.gameObject.GetComponent(MyScript); // MyScript = the name of the script
     myScript.hitPoints = 100; // var (of type int) on myScript
     myScript.DoSomething(); // function on myScript

EDIT:

one good way would be to just use the distance of mouse movement to apply the rotation:

 var lastMousePos : float;
 var currentMousePos : float;
 
 function Start()
 {
 currentMousePos = Input.mousePosition.x;
 lastMousePos = Input.mousePosition.x;
 }
 
 function Update(){
 currentMousePos = Input.mousePosition.x;
 var thisFrameRotate = currentMousePos.x - lastMousePos.x;
 transform.Rotate(Vector3.up, thisFrameRotate);
 lastMousePos = Input.mousePosition.x;
 }

not tested but should work, at least to give an idea

and glad to help!

Comment
Add comment · Show 1 · 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 SergeantBiscuits · Jul 22, 2012 at 09:54 AM 0
Share

Thank you so much; I had seem similar solutions but for some reason the way you laid it out finally made it click in my head. Starting to understand how this javascript stuff works, haha. Thanks again!

avatar image
0

Answer by xSpectrum · Jul 22, 2012 at 10:02 AM

Well from what I gather from your post, you could do something like:

  var movementScript = transform.parent.GetComponent("Movement");
  turretAngle = movementScript.directionAngle;
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

7 People are following this question.

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

Related Questions

Make a simple tree 1 Answer

Can't access variable in another game object. 2 Answers

Getting a Trigger's parent 1 Answer

How can i make GetComponent(SCRIPT) apply to ONLY ONE object that has the script? 1 Answer

Putting a variable of a child in the inspector for the parent 1 Answer


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