- Home /
Why does my variable show a rising score AND 0 at the same time?
I'm making a score counter and letting other objects access it with Get and Set functions. The Inc(n) function works and "score" keeps going up, but GetString() shows "score" at 0. Why is that happening?
 #pragma strict
 private var score:int;
 
 function Start () {
     score = 0;
 }
 
 function Inc(a:int) {
     print("inc added "+a+" to score.");
     score = score+a;
     print("Score is now "+score+".");
 }
 
 function GetString():String {
     print("Sending score as "+score);
     return score.ToString();
 }
 
 function Get():int {
     return score;
 }
 
 function Set(a:int) {
     score = a;
 }
 
 
Additional - here is a shortened version of the script that uses the Inc(n) function...
 #pragma strict
 // abridged code
 var scoreManager:ScoreManager; // This is so you can send stuff to the score manager.
 // abridged code    
 function OnTriggerEnter(col:Collider)
 {
     for (a=0; a<objectsThatCanKillYou.length; a++){
         if (col.name==objectsThatCanKillYou[a].name){
         // abridged code
             // destroy object if damage goes below 0
             if (instanceDamage<=0) {
                 // Add to score
                 scoreManager.Inc(pointsForKillingMe);
                 // Spawn death Anim
                 ObjectPool.instance.Spawn(deathGameObject.name, trans.position, trans.rotation, true);
                 ObjectPool.instance.Unspawn(gameObject);                
             }
         }
     }
 }
And here's the script that's using GetString().
 #pragma strict
 private var textSprite:exSpriteFont;
 var scoreManager:ScoreManager;
 
 function Start () {
     // Initialisation
     textSprite=gameObject.GetComponent(exSpriteFont);
     // Main loop
     while (true)
     {
         print(scoreManager.GetString());
         textSprite.text = scoreManager.GetString();
         yield WaitForSeconds(0.125);
     }
 }
 
I dont see anything wrong, but you wrote a function which returns the string, so i guess you should also use it. At inc write either print("Score is now"+score.ToString()+".") or print ("Score is now"+GetString()+".") even though this function is a bit unnessesary in my eyes as you can write .ToString() whereever you need to.
Thanks ValooFx. I tried doing that earlier. It gave me a more puzzling result: "Score is now CompilerGenerated.Score$$anonymous$$anager_Inc$callable0$11_37.". $$anonymous$$aybe it's showing the pointer to the variable?
well, I just tested your script as is and it all works perfectly, so you must be calling it wrong somehow...
see for yourself, just add this to the top of the script:
 private var score:int;
 var publicScore : String;      //from here
 function Start () {
     score = 0;
     Inc(4);
     publicScore = GetString();   
 }                       //to here
 
 function Inc(a:int) {
all three prints and the variable all return "4"
@Billamu, Show us the script you are using to increment, set and get the score
@Jeffom I just updated my post show it's showing the other scripts referencing this one. The collision detection script works fine. It's the one sending the score to ex2d that keeps sending back 0's.
Answer by Jeffom · Nov 16, 2012 at 11:47 AM
You should try to set the score reference at runTime, i think the best way would be to take the monobehaviour from the scoremanager and instantiate it into your main class and update the class from there.
Answer by Billamu · Nov 16, 2012 at 11:53 AM
Qapla' (Success)!
The script was fine, but it was referencing a gameobject with my script on it inside the project bin, not the heirachy... in short, there were 2 instances of my script and they weren't referencing the one I was using at run time.
I modified one of my scripts to explicitly point to the one in the heirachy at runtime.
 #pragma strict
 private var scoreManager:ScoreManager; // This is so you can send stuff to the score manager.
 // Yadayadayada
 function Start () {
     scoreManager=GameObject.FindGameObjectWithTag("scoreManager").GetComponent(ScoreManager);
 //... and now it works.
So basically what @jeffom said, but 7 minutes later than what he posted XD.
Your answer
 
 
             Follow this Question
Related Questions
What are these things doing in C#? 3 Answers
Are properties variables? 1 Answer
Variable value doesn't change 1 Answer
changing variables values !! 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                