• 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
Question by xefhs · Jul 03, 2012 at 03:45 PM · share

What is your own method to share data between script ?

Hi,

When you have some datas (by data I mean variables) you want share them in many scripts, what do you do ?

I don't know if I should create a static class to gather all datas or create a Script and attach it to a kind of "Master GameObject" in my scene and catch him after with GetComponent() ?

None of these two methods seems right to me... so what is your own method to share data between script ?

Comment

People who like this

0 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by delstrega · Jul 03, 2012 at 03:56 PM

Actually, if you have a static class and use static variables you don't have to "find" it or use GetComponent(). You can access the vars directly through the classname. At least that's the way I do it. You might want to take a look at a Singleton approach:

http://unifycommunity.com/wiki/index.php?title=Singleton http://unifycommunity.com/wiki/index.php?title=AManagerClass

Comment
sacredgeometry
Adamcbrz

People who like this

0 Show 0 · 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

Answer by ahsen35813 · Mar 27, 2020 at 10:45 PM

Actually, this can be done quite easily. The solution can be found here: https://answers.unity.com/questions/42843/referencing-non-static-variables-from-another-scri.html I certainly found it extremely helpful, and it should answer your question.

In a nutshell, make the variable you want to access from the other script public like this:

 public float myVariable = 3.14159;

Then, you need to put this code in the script that will be acessing that public variable

 private float myOtherVariable;
 private GameObject gameObjectWithVariable;

 void Start() 
 {
     gameObjectWithVariable = GameObject.Find("TheNameOfTheGameObjectThatContainsTheScriptWithTheVariableYouWant");
     myOtherVariable = GetComponent<TheNameOfTheScriptThatContainsThePublicVariable>().myVariable;
 }
 void Update() {
     myOtherVariable = GetComponent<TheNameOfTheScriptThatContainsThePublicVariable>().myVariable;
     Debug.Log(myOtherVariable);
 }

The update block is only necessary if you need to detect changes in the variable. If you just need to access it once, only having it in the Start() block will work.

Comment
sacredgeometry
Adamcbrz

People who like this

0 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 Adamcbrz · Mar 28, 2020 at 02:37 AM 2
Share

I don't want to be rude but please don't ever use GameObject.Find in an Update Loop. That implies your are going to search your entire Scene gameobject by gameobject until you find that element. To do that on Update would kill your frame rate. Do as you suggested and store a class reference in Start() then in update just reference that class.variable.

 private MyCustomClass myCustomClass;
 
 void Start()
 {
     myCustomClass = GameObject.Find("MyCustomClassObject").GetComponent<MyCustomClass>();
 }
 
 void Update()
 {
      var myOtherVar = myCustomClass.awesomeVariable;
 }
avatar image ahsen35813 Adamcbrz · Apr 01, 2020 at 04:10 PM 0
Share

Good point, thank you very much for telling me about that. I've revised the code so I hope that's better.

avatar image

Answer by sacredgeometry · Mar 29, 2020 at 01:33 PM

Depending on what objects you are talking about you can get the references to significant game objects on startup or instantiation and keep those references in memory.

Then you can call methods on those objects or set properties/ fields on those objects to pass data between them.


Static members and singletons should be used sparingly (i.e. when appropriate) as they can cause all sorts of issues with threading and breaking the testability of your codebase.

If you really want to use them then at least consider using an IoC container and injecting them into your components as needed.

https://github.com/unitycontainer/unity


Let me know if any of that is unclear and I can provide examples.

Comment

People who like this

0 Show 0 · 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.

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

Share results or score on google+ from android unity3d 1 Answer

Choose an App to share - option in Unity 3D 1 Answer

How Do Teams Works on TheSame Scene/Project 1 Answer

Share Screenshot Image to Facebook with Facebook SDK 0 Answers

share or reassign materials 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