- Home /
Programming knowledge. Solved via comment.
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!
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.
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.
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!