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 /
This question was closed Apr 27, 2015 at 07:09 PM by fafase for the following reason:

Programming knowledge. Solved via comment.

avatar image
0
Question by Kinger61 · Apr 27, 2015 at 06:04 PM · c#variablefunction

Getting a Non Static Variable From a Different C# Script

I have two scripts. One has a variable with a value set to it contained within a function. I want to get that variable to use it in another script. This is the script with the variable i need. (i cant make it a public static int due to the resource money at the top being used for other countries within the same script). There is more so the class is closed incase your wondering. The rest is irrelevant to what I want so didnt include it.

 public class MoreResourceUI : MonoBehaviour 
 {
 
     public static int resourceMoney;
     public static int resourceTroops;
     public static int resourceWeapons;
     public static string resourceAllegiance;
 
     //Assign the Needed Money to the Countries
     public void asiamoney (Text asiaMoney)
     {
         int AsiaMoney = 0;
 
         resourceMoney = AsiaMoney;
         AsiaMoney = 400;
 
         asiaMoney.text = "Money: " + AsiaMoney; //Output required Money Amount
 
     }

This is the other script in which I am trying to get the variable AsiaMoney for so that it can be used to compare with another variable.

 public class Asia_Resources : MonoBehaviour 
 {
 
     public static int Money_Asia;
     public static int Troops_Asia;
     public static int Weapons_Asia;
 
     public MoreResourceUI money;
 
     void Awake()
     {
         Money_Asia = 0;
         Troops_Asia = 0;
         Weapons_Asia = 0;
     }
     
 
     void Update () 
     {
         Money_Asia = SliderUIMoneyTextUpdate.sliderMoneyValue;
 
         money = GetComponent<MoreResourceUI> ();
 
         MoreResourceUI moreResourceUI = GetComponent<MoreResourceUI> ();
         moreResourceUI.AsiaMoney;
 
         if(Money_Asia == AsiaMoney)
         {
 
         }
     
     }
 }


Any ideas as to what i can do ? As you can see I have tried to get component. (both scripts are attached to the same empty gameObject also.) Thanks!

Comment
Add comment · Show 1
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 fafase · Apr 27, 2015 at 07:08 PM 0
Share

You declare Asia$$anonymous$$oney within a method so it is local to that method and doe snot exist outside of it. Declare it in the class, just like your static variables but without static and this is it.

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by hbalint1 · Apr 27, 2015 at 07:06 PM

you can't reach a local variable in a script. make int AsiaMoney to public in MoreResourceUI, so you can reach it:

  public class MoreResourceUI : MonoBehaviour 
  {
      ...
      public int AsiaMoney;
  
      //Assign the Needed Money to the Countries
      public void asiamoney (Text asiaMoney)
      {
          AsiaMoney = 0;
          ...
      }

now you can reach it from other script with GetComponent().AsiaMoney and you don't need to make it static.

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
0

Answer by jodev · Apr 27, 2015 at 10:16 PM

Given the code you have posted above, there is no way to get the value of the AsiaMoney integer. Here's why. The AsiaMoney variable is a local variable within the asiamoney() method. You call the method, the AsiaMoney variable is brought into live, then used. However, once you return from the method, the variable ceases to exist.

So you'll need to make something public. You said you can't make the AsiaMoney variable public. But the code that you supplied doesn't really tell me why, because I'm not sure what you are trying to accomplish in the asiamoney() method. With the code given, you assign it a value of zero, then assign that value of zero to resourceMoney and assign 400 to AsiaMoney. In other words, the code above is essentially the same as this:

 public void asiamoney (Text asiaMoney)
 {
     resourceMoney = 0;
     asiaMoney.text = "Money: 400"; //Output required Money Amount
 }

Nothing is really changing, which is probably not what you want. Maybe you can eleborate a bit, so I can update my answer accordingly?

So yes, if you want to access a variable or method from another class, it must be public. Again, I'm not sure what you AsiaMoney should be doing, but if you don't want to create a public field, you could consider a public property instead. You can make that read-only and it could look something like this:

 // We use a private backing field to store the AsiaMoney value.
 private int asiaMoneyBackingField = 0;

 // The property can be read from any class, but can only be set from this class.
 public int AsiaMoney
 {
     get { return asiaMoneyBackingField; }
     private set { asiaMoneyBackingField = value; }
 }

 // Assign the value 400 to AsiaMoney.
 public void asiamoney (Text asiaMoney)
 {
     AsiaMoney = 400;

     // Any other code
 }

To access the property, you can use the the code you already have with a minor addition:

 MoreResourceUI moreResourceUI = GetComponent<MoreResourceUI> ();

 if (Money_Asia == moreResourceUI.AsiaMoney)
 {
 }

The moreResourceUI variable now holds an instance of the class. Since AsiaMoney is now a public property of that class, you can access it like you already did. I just moved it into the if-statement.

Hope that helps. If I need to update my answer, please eleborate a bit more on what you are trying to do. Good luck!

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

Follow this Question

Answers Answers and Comments

21 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

Related Questions

When issuing to print a private variable from the script, the script prints all the variables attached to objects using the same script instead of the specific object. How do I fix this? 3 Answers

summing up items price in runtime 1 Answer

C'# When Taking Variables From Other Scripts, The Values Do Not Update 1 Answer

What is this C# code in javascript 0 Answers

Multiple Cars not working 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