• 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 /
  • Help Room /
avatar image
-1
Question by RecyclingBen · Dec 22, 2016 at 02:34 AM · parent

Access variables from parents

I've tried everything. Everything. There are so many posts, and none of them work. How do I access variables from a parent class. I need help!!

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
0

Answer by RecyclingBen · Dec 23, 2016 at 07:19 PM

I figured it out. I had to make a game object in the script that was taking the variables, and then apply the script I wanted to take variables from to that game object through the inspector

So something like this

Private Example example;

GameObject examplevariables;

Start() { example = examplevariables.GetComponent(); } Thanks to all who helped

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 RecyclingBen · Dec 23, 2016 at 07:20 PM 0
Share

Also, this works for any scripts, not just variables from parents

avatar image
0

Answer by Vectorizing · Dec 22, 2016 at 04:40 AM

A variable in a class that is attached to the parent?

If that is the case you could use do something like:

 gameObject.transform.parent.gameObject.GetComponent<SCRIPT_NAME>().Variable..

Makes sense?

Comment
Add comment · Show 7 · 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 im012 · Dec 22, 2016 at 06:13 AM 0
Share

The Variable that you want to access must be a public Variable.

avatar image RecyclingBen · Dec 22, 2016 at 08:32 PM 0
Share

Where do I put that script?

avatar image jmgek RecyclingBen · Dec 22, 2016 at 09:03 PM 1
Share

Hey @RecyclingBen while I understand you're trying to learn (and it's great to learn) asking basic questions like this won't help you in the long run.

GetComponent(); is a basic function in unity that is easy to do. If you're having trouble with this (and even knowing where to put it "Where do I put that script?") when you have some time please do some basic tutorials that will $$anonymous$$ch you the beginning stages of Unity and C#.

https://unity3d.com/learn/tutorials/projects/space-shooter-tutorial

It's great to see you learning but you have to understand the basics of Unity first.

:)

avatar image RecyclingBen jmgek · Dec 22, 2016 at 10:18 PM 0
Share

I understand, and I'll find other means of getting answers to my questions. Don't expect me to be gone, though. I'll be on these forums whether you like it or not ^^

Show more comments
avatar image RecyclingBen · Dec 23, 2016 at 04:29 PM 0
Share

Thanks, @Vectorizing, but did it work for you? it didn't work for me. Here is my script

gameObject.transform.parent.gameObject.GetComponent( ).leftRotation;

avatar image Vectorizing RecyclingBen · Dec 23, 2016 at 04:49 PM 0
Share

Can you attach a picture of both scripts and which variable you are trying to access?

Thanks

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Reading a var from variable parents 1 Answer

Child to parent behave weird 0 Answers

AI script that makes object follow the player through collision 0 Answers

Destroy one child (of same instance) after another? 0 Answers

transform.parent not working? (Unity 2d) 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