Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Tritize · Jun 15, 2018 at 10:04 PM · rigidbodyvariableaccessprivate

Best way to access a private variable from another script (same GO) ?

Hi,

So I have script A and script B on the same GameObject, that have a rigidbody. In script A I have a private variable type Rigidbody that I assign in Start function with GetComponent. Now I want to access the rigidbody from script B. I found a lot of methods online but as I'm new I don't know which is the best and I would take good habits.

My first though was assigning it as well in script B same as script A, but idk it do not seems very optimized? Or I can change from private to public static variable? Or in the start of script B I can use a GameObject.Find then a GetComponent? I'm learning Unity so I would really now how someone good at programming would do for accessing the rigidbody in that case :)

Thanks !

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
4
Best Answer

Answer by Nebukam · Jun 15, 2018 at 10:31 PM

By definition private members are not accessible from outside the class itself. Best way to go is to make it public in A so you can access it from B, however be aware that this is an implicit dependency (meaning if you have script B but not A on a GO, B won't be happy). You can make sure A is always present when using B via RequireComponent to make the dependency explicit : https://docs.unity3d.com/ScriptReference/RequireComponent.html

There's other ways (many other ways !) to achieve the same result, it all boils down to how your project is architectured and what kind of interactions you want between them. There's not "bad" solution per-say, although GetComponent is way faster than GameObject.Find. Since you know what you're looking for, and it should exist, GetComponent is the way to go.

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 Tritize · Jun 16, 2018 at 07:20 AM 0
Share

Thanks for that answer! Ok so I might make it public and use GetComponent :)

Since in every tutorial I was seeing the rigidbody as a private variable I was wondering if there was an issue with it being public. Also, do I have to make it a static public variable or just a public will do the trick?

avatar image Nebukam Tritize · Jun 16, 2018 at 05:52 PM 0
Share

Public is fine, static is aimed at making your variable available pretty much everywhere in your code and comes with other constraints (roughly, always point to the same memory address, no matter the instance)

avatar image
0

Answer by chetan312 · Jun 16, 2018 at 07:03 PM

if the both scripts are on same game object, you can use GetComponent<>() command otherwise first access the other Gameobject by using GameObject.FindWithTag () and then access the script attached to it.

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

121 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can you make a variable modifiable ONLY by the Inspector? 2 Answers

Script not responding to public variable change 1 Answer

Accessing variable from a method in another script and gameObject 2 Answers

Access variable from another script? Health! 3 Answers

Can I access variables of scripts that inherit from abstract classes? 2 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