Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Billamu · Nov 16, 2012 at 10:38 AM · functionvariablesgetsetscope

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);
     }
 }
 
Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Polymo · Nov 16, 2012 at 10:51 AM 0
Share

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.

avatar image Billamu · Nov 16, 2012 at 10:58 AM 0
Share

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?

avatar image Seth-Bergman · Nov 16, 2012 at 11:00 AM 0
Share

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"

avatar image Jeffom · Nov 16, 2012 at 11:01 AM 0
Share

@Billamu, Show us the script you are using to increment, set and get the score

avatar image Billamu · Nov 16, 2012 at 11:21 AM 0
Share

@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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Billamu · Nov 16, 2012 at 11:56 AM 0
Share

Yep, I got there eventually. Thanks a lot! :-D

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How do I set and get the position of a object? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges