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 maxnorth · Jan 07, 2015 at 09:31 PM · how-tostatic-variables

How can I efficiently access a particular game object's script's member variables from within another script?

I know this question sounds like a duplicate but I really have looked around already, and if you read on you'll see I'm aware of at least one method but want to know of others.

What is the best way to manipulate a variable within a specific game object’s script? I’ve learned that the problem with setting the variable to ‘public static’ and accessing the variable using ‘exampleScript.desiredVariable’ is that a static member acts as a universal member for the class, whereas I need to access a unique value for a given instance of that script.

The one method I am aware of is creating a new variable of the type ‘exampleScript’ within the script that would like to access ‘desiredVariable,’ because I can then assign a particular game object’s script to this variable and access and modify the members of the particular script by modifying members of the variable.

But what else is there? I am a little concerned that this is going to use up unnecessary space. Why create a new variable which contains all the data of MonoBehaviour plus whatever variables and methods I have written in the script, when that data (presumably) exists somewhere on the RAM already? Is there no way to access it without creating a duplicate instance? Is this as unnecessarily wasteful as I’m thinking it is, or I overestimating the overhead involved here? Please help, and thanks 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

2 Replies

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

Answer by DanSuperGP · Jan 07, 2015 at 09:39 PM

It doesn't make a copy.

It's just a reference to the other Monobehavior. It just points to the memory location of the script that you have assigned to the variable.

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 tanoshimi · Jan 07, 2015 at 09:47 PM 2
Share

This is correct. Script components are passed by reference, not by value. Dragging a script into a public variable of a gameobject, or storing a reference via GetComponent does nothing other than create a pointer to the existing copy of that component.

avatar image maxnorth · Jan 08, 2015 at 02:27 AM 0
Share

Perfect, this is reassuring. Thanks!

avatar image
0

Answer by Graham-Dunnett · Jan 07, 2015 at 09:31 PM

http://docs.unity3d.com/Manual/ControllingGameObjectsComponents.html

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 maxnorth · Jan 08, 2015 at 02:29 AM 0
Share

This was also very helpful, thanks. I'm actually mostly excited that it led me to the page on coroutines. Coroutines! So cool!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How would I change the value of one prefab's variable via script without changing all of them? 1 Answer

Can't load monodevelop?! 2 Answers

How can I enable and disable scripts at runtime? 2 Answers

Error: not a member of 'UnityEngine.GameObject[]'. 2 Answers

Overload Method problem 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