- Home /
How to keep variable value
I have a piece of code that I have attached to many game objects. In this script, the player is supposed to enter the meaning and gets 5 points for the correct answer.
private var inRange: boolean = false;
var myStyle = new GUIStyle();
var score:int;
public var mess: String = "Clue";
var stringToEdit : String = "Remove this and enter meaning";
var finalstring:String="nil";
function OnTriggerEnter(other:Collider)
{
if (other.tag == "Player")
{
inRange = true;
}
}
function OnTriggerExit(other:Collider)
{
if (other.tag == "Player")
{
inRange = false;
}
if(score==5)
{
score=5;
}
}
function Update(){
if(finalstring==stringToEdit)
{
score=score+5;
}
if(score==5)
{
stringToEdit="null";
}
}
function Start()
{
if(score==5)
{
score=5;
}
}
function OnGUI ()
{
if(inRange)
{
GUI.Label (Rect (10, 10, 100, 20),"Score: " +score.ToString());
// Make a text field that modifies stringToEdit.
stringToEdit = GUI.TextField (Rect (10, 50, 1000, 20), stringToEdit, 25);
GUI.Label (Rect (10, 30, 2000, 1000),"Clue: " +mess,myStyle);
}
}
The problem is that whenever the player moves on to the next object after getting 5 points, the score resets to 0. how to keep the score as it is
Answer by Jamora · Jan 18, 2014 at 05:29 PM
It's not that the score resets on each Game Object, it's that you save the score on a single GameObject-basis. This means each GameObject has a different idea of what the score is.
To remedy this, the simplest solution is to use a static variable. So, instead of var score:int;
have static var score:int;
. Static
means that the same value is shared between all instances of that class.
Several other things about your code, I feel need pointing out, are the if blocks on lines 43 and 21. Setting score to 5 if it already is 5 seems unnecessary to me. Further, your Update method will work better if it's:
if(finalstring==stringToEdit)
{
score=score+5;
stringToEdit="null";
}
This way it'll change stringToEdit
each time a correct answer is entered, as opposed to when the correct answer is entered and the score is 5 (causing the score to increase indefinately after the first correct answer).
One more problem I see with this design, is that the player is able to answer a question multiple times, each time adding 5 to score. The simplest solution is to use an isAnswered
boolean flag, which is set to true within the above if clause and checked to be false before awarding any points.
Thnks! Just what i was looking for (set the var to static to keep a simple collecting items counter working with multiple scenes) :)
Answer by And0 · Jan 18, 2014 at 06:40 PM
As far as I can tell the problem is that each object has its own score
variable and draws its own label.
I would create a separate static object containing a variable to handle the total score and to draw the label. Then each object could pass its score value over to that scoreCounter object (or whatever you want to call it) where it is then added to the total score.
Answer by bodec · Jan 18, 2014 at 07:12 PM
You need a solid gameobject to carry over from scene to scene at the start of each scene all previous gameobject are destroyed in a start functionon a gameobject you keep use DontDestroyOnLoad() I think is the command and you won't loose the data
Your answer
![](https://koobas.hobune.stream/wayback/20220613130801im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
int variable loses its value 0 Answers
How to return to a value used before? (C#) 1 Answer
How do I reset a variable? 2 Answers
assign value for all the script 2 Answers