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 /
  • Help Room /
avatar image
2
Question by alanmeana · Oct 03, 2016 at 06:01 AM · variablesclassesimporting

how do I read a variable from another class c#

I want to read a string variable from another class like this:

public class Startmenu : MonoBehaviour {

 public string myText;  // 

I want to use the content of this variable myText, from this class Startmenu in another class

public class Move : MonoBehaviour {

 // Use this for initialization
 void Start () {
     }

I want to read the variable string myText from Startmenu class in Move class, nothing too complicated just get that string so I can use it to name something in the class Move

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Rs · Oct 03, 2016 at 09:04 AM

You can read any public variable of a class from any other class. In your case

 public class StartMenu : MonoBehaviour {
     public string myText;
     void Start()
     {
         //
     }
     void Update()
     {
         //
     }
 }

You can access StartMenu's field called myText as seen here in the Start function:

 public class Move : MonoBehaviour {
 
     public StartMenu startMenuReference; // in Inspector drag and drop here the obejct that holds the StartMenu component
    
     void Start()
     {
         if (startMenuReference == null)
             Debug.LogError("Start Menu not given"); // just remind yourself to fill the slot.
         else
             Debug.Log(startMenuReference);
     }
     void Update()
     {
         
     }
 }

And that's the most straight forward. If you are sure you'll always have just one StartMenu instance you can use the static instance method that many developers use in many occasions.

It looks like this:

 public class StartMenu : MonoBehaviour {
      public string myText;
 
     public static StartMenu instance;
 
      void Awake() // note it's now Awake instead of Start. That's important.
      {
          instance = this; // hold a reference to the (last) instance of this class
      }
      void Update()
      {
          //
      }
  }

And in that case your Move class would change as follows:

  public class Move : MonoBehaviour {
  
      StartMenu startMenuReference; // in Inspector drag and drop here the obejct that holds the StartMenu component
     
      void Start()
      {
          startMenuReference = StartMenu.instance;
          if (startMenuReference == null)
              Debug.LogError("Start Menu does not exist"); // No instances found
          else
              Debug.Log(startMenuReference);
      }
      void Update()
      {
             //
      }
  }
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
avatar image
1

Answer by SaurabhStudio · Oct 03, 2016 at 08:11 AM

 Startmenu  startmenu = GameObject.Find("GameObjectName").GetComponent<Startmenu>();
 
 Debug.Log(startmenu.myText);
 
 
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
avatar image
1

Answer by nksG · Oct 03, 2016 at 02:03 PM

Most quick way is to declare your variable as a static one.

 public static string myText;

Then, you can access it and edit it from anywhere like this:

 StartMenu.myText = "a new string value";

  • Static variables are not visible in the inspector.

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 nicxellent5 · Nov 16, 2018 at 02:55 AM 0
Share

are there any side effects to making it static?

avatar image Hellium nicxellent5 · Nov 16, 2018 at 06:37 AM 0
Share

Yes. A static variable is shared by all the instances of the class. For instance, if you have a public static into Life declared in an enemy class, every enemy will have the same amount of health point. Hurting one will hurt all the others. It's rarely the desired effect in the end.

avatar image
0

Answer by shawww · Oct 03, 2016 at 02:06 PM

Another way to do it.

Add this variable to the top of your script:

public Startmenu _startmenu;

Go back to Unity, and click on the object that has this script attached. In the inspector, you'll see a space where you can drag a gameobject that has the Startmenu script attached to. If you drag the game object, it'll automatically put the script into the box.

It's totally fine for you to then manipulate startmenu.myText -- BUT good coding practice usually says that you should make your variables private and use getters and setters to access them. So you might have something like:

private myText = "";

public void setMyText(string t){ myText = t; }

public string getMyText(){ return myText; }

Hope this helps!

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

9 People are following this question.

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

Related Questions

Managing In-Game Variables of Different Data Types 1 Answer

Random.Range for player stats 0 Answers

Grouping variables question 1 Answer

How to integrate a C# Project in Unity 1 Answer

How do i change a variable from one Class to another Class? 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