How do you call variables within a method on a static class?
This is a very basic question for anyone in here but I am very new to programing so please excuse me if this is a silly question.
I have a static class LevelManager and in it i have an intialize method
public static void initalize()
{
int admin_Gold = 1000;
int admin1_Gold = 500;
string UserName = "Admin";
string UserName2 = "Admin2";
}
In my other class called LoginManager I am trying to use UserName and UserName2 by writing:
LevelManager.UserName
public void LoginFunction()
{
string UserName = LevelManager.UserName;
string UserName2 = LevelManager.UserName2;
if (userName == UserName && passWord == "password"
|| userName == UserName2 && passWord == "password1")
{
Application.LoadLevel("MainMenu");
}else{
Debug.Log("Else Statement");
};
}
What is the proper way to write this? I am receiving this error: "LevelManager does not contain a definition for 'UserName'"
You said the static class is called GameControl, so why are you referencing class Level$$anonymous$$anager ins$$anonymous$$d of class GameControl?
EDIT: I see you've updated your description.
Answer by Positive7 · Sep 03, 2015 at 06:54 AM
public static void initalize(ref string UserName1, ref string UserName2, ref int admin1_Gold, ref int admin2_Gold)
{
UserName1 = "Admin1";
UserName2 = "Admin2";
admin1_Gold = 1000;
admin2_Gold = 500;
}
public void Start()
{
string str1;
string str2;
int int1;
int int2;
initalize(ref str1, ref str2, ref int1, ref int2);
Debug.Log(string.Format(str1 + " {0} " + str2 + " {1} ", int1, int2));
}
Hi @Positive7 Thanks for your response. I just want to make sure I understand your example. Is the Start() in this case suppose to be in the login class?
Thank you.
Yes! You can use the values where ever you want it. Let me know btw.
@positive7 Thanks! I see an error for each variable saying a local variable cannot be decalred in this scope because it would give a different meaning to 'ad$$anonymous$$Gold', which is already used in a 'parent or current' scope to denote something else. Do you know why this happens and how i can fix it?
There's nothing wrong. $$anonymous$$aybe because you never use the int values. No need to return anything. The compiler just says that you are not doing anything with those variables other than assigning them.
if you put the
string str1;
string str2;
int int1;
int int2;
inside the LoginFunction()
method as I did in Start()
the Warning will go away.
@positive I have the following in my login function
string str1;
string str2;
int int1;
int int2;
Level$$anonymous$$anager.initalize(out str1, out str2, out int1, out int2);
Answer by Cam Edwards · Sep 07, 2015 at 02:49 PM
While you already seem to have solved your problem, I'm leaving an answer to clarify things a little bit. When you write code like the following:
public static void Initialize()
{
int admin_gold = 1000;
}
The variable admin_gold is only usable inside that function. When Initialize() is finished, that variable effectively disappears. That's because all variables have what's called a Scope, that determines where they can be accessed from and how long they exist for.
You weren't wrong thinking that you should be able to access your variables by calling LevelManager.admin_gold, the only problem was that the variables were defined in the wrong place. For you to be able to access those variables like that, your code should have looked like this:
public class LevelManager
{
public static int admin_gold;
public static int admin1_gold;
public static void Initialize()
{
admin_gold = 1000;
admin1_gold = 5000;
}
}
The only difference is that now those variables live in the LevelManager class, not the Initialize method. You can now access and assign values to those variables from anywhere else in your game using LevelManager.admin_gold, just make sure that you call LevelManager.Initialize() before you try to access them!
That was very informative thank you so much for taking the time to explain this to me!
I have an additional question for you, Lets say in the next scene I want to be able to update the value for gold and save it (for example I have a function called sell which will add 50 gold) - I want to be able to save this update within my static class. - I am assu$$anonymous$$g that the way initalize currently works it would over ride any changes with 1000. Is that correct? How can I make it save any changes to the amount?
Also I was wondering if you could recommend any resources on understanding scope and OOP in general - I am pretty new to all of this stuff. Thank you!
You're right that every time you call initialize, the variables will have their values overwritten with whatever values you use in the initialize function yes. Luckily, loading new scenes doesn't change values stored in static variables. This means that you can call initialize once and only once when your game starts up, and then any changes you make to the value in one scene will carry over to the next one. If you want the changes to carry over between quitting the game and running it again, you'll have to think of something more clever.
I picked up a lot of what I know from making different games and projects. Your first few games will probably never be finished, but you learn something with each one. I personally learned a lot of the fine details and more complex parts about program$$anonymous$$g from this book; it's the textbook for introductory program$$anonymous$$g at my university, but there are probably equally good free resources (somewhere!).
I can't recommend enough just building things and googling / asking questions here when you get stuck, as long as you can live with the fact that for a while, you won't be making anything ground-breaking.
Your answer
![](https://koobas.hobune.stream/wayback/20220612002438im_/https://answers.unity.com/themes/thub/images/avi.jpg)