- Home /
The question is answered.
Changing static variables from another script?
Hello,
how can I change a static variable from another script? I would like to have an Option-Menu, where you can change like the amount of an ammo (for a little shooting Game).
Here is my script (called global) for the static variable of the ammo:
#pragma strict
static var ammo : int = 5;
static var obstacleCount : int;
Does anyone have a solution?
How it will look like, when I use an input field or something else?
global.ammo = GUI.TextField (Rect (10, 10, 200, 20), global.ammo, 25);
Here is my Option-Script, which I have an Error:
global.ammo = GUI.TextField (Rect (10, 10, 200, 20), global.ammo,10);
if (GUI.Button(Rect(10,10,200,20),"Spiel starten"))
Application.LoadLevel("$$anonymous$$iShooter");
The Error is:
No appropriate version of 'UnityEngine.GUI.TextField' for the argument list '(UnityEngine.Rect, int, int)' was found.
you need to convert your ammo, of type int, to a string, TextField arguments are (UnityEngine.Rect, string, int).
So change your code to:
global.ammo = Convert.ToInt32(GUI.TextField (Rect (10, 10, 200, 20), global.ammo.ToString(),10));
I'll be honest with you though, if text, alpha characters, non-numeric characters are entered, it will throw an exception. You may want to use a temporary variable that holds the conversion. If you don't need to update the variable in the textfield and it's for display, use a label, which is read only. You will still need to .ToString() the ammo because the expectation is a string not an int.
I have an another error:
$$anonymous$$ identifier: 'Convert'.
Follow this Question
Related Questions
One script for all variables? 0 Answers
Static variable issue in published 1 Answer
My static variable is not changing 2 Answers
Unable to modify a variable in another script 2 Answers
Global Variables Refuse to Cooperate 1 Answer