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
1
Question by Salitas · Nov 20, 2013 at 04:55 PM · scoreintcounterinteger

int +=1 won't exceed 1

Hello there, I'm having problems with my score script for my game The score won't ever exceed 1,

It's supposed to add 1 to the score gui on Trigger enter, i have the code compounded on the coin itself

The update from 0 to 1 works but no matter how many coins i pick up it always stays 1 The Debug.Log was simply to test if it was the GUIText screwing up, but it has to be the integer... Here's the code

 #pragma strict
 public var score : int = 0;
 var guiScore : GUIText;
  
 function Start ()
 {
         guiScore.text = "Score: 0";
 }
  
 function OnTriggerEnter (info : Collider)
 {
         if (info.tag =="Player")
         {
                 Destroy(gameObject);
                 score += 1;
                 guiScore.text = "Score: " + score;
                 Debug.Log("The value is: " + score);
         }
 }
Comment
Add comment · Show 2
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 fffMalzbier · Nov 20, 2013 at 05:03 PM 0
Share

$$anonymous$$aybe its a problem that you destroy the gameObject ?

avatar image Salitas · Nov 20, 2013 at 05:05 PM 0
Share

I tried it with destroying the object after adding the score but that didn't work out either

Just tried getting rid of the destroy completely and that worked, so it must be destroying the object before executing the script properly

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by meat5000 · Nov 20, 2013 at 05:09 PM

You Destroy the gameobject the score modifier is attached to. It will work once then be destroyed.

As score is a variable you create in that script it too will be destroyed.

Make your score a variable on some non destroyed object and use GetComponent to modify it instead?

 var scoreboard : GameObject; //for instance of scoreboard object
 
 scoreboard = GameObject.Find("ScoreBoard"); //Link instance to the actual ScoreBoard object you place in hierarchy, Performed in Start()
 
 scoreboard.GetComponent(Score).score += 1; //line used to modify the score.
             

Create a ScoreBoard gameobject in hierarchy and place a script on it with a

 public var score : int = 0;
Comment
Add comment · Show 3 · 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 Salitas · Nov 20, 2013 at 05:24 PM 0
Share

Oh, yeah that sounds like it should work.. Thanks a bunch!

avatar image Salitas · Nov 20, 2013 at 05:29 PM 0
Share

Works like a charm! Thanks a bunch! :D

avatar image meat5000 ♦ · Nov 20, 2013 at 05:34 PM 0
Share

You are welcome :)

avatar image
0

Answer by aldonaletto · Nov 20, 2013 at 05:18 PM

Is this script attached to the coins? If so, the problem is the score variable: there exists one score in each coin instance, thus the coin hit increments its own score and dies. A simple solution is to declare score a static variable:

 public static var score: int = 0;

Static variables are created when the game starts and survive til its end. They are the closest equivalent of global variables in other systems (a traditional global variable isn't only globally accessible, but also lasts for the whole application life).

A more versatile alternative is to declare score in the GUIText script, and modify it via SendMessage. The GUIText script could be as follows:

 public var score: int = 0;

 function Start(){
   AddScore(0); // force text to update to current score
 }
 
 function AddScore(points: int){
   score += points;
   guiText.text = "Score: "+score;
 }

Just call AddScore via SendMessage in the coin script:

 var guiScore : GUIText;
  
 function OnTriggerEnter (info : Collider){
   if (info.tag =="Player"){
     // call AddScore(1) in a script attached to guiScore:
     guiScore.SendMessage("AddScore", 1);
     // suicide coin:
     Destroy(gameObject);
   }
 }
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

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Uniting 2 integers from two different sources 1 Answer

How do i link a GUI text to a variable text which is a integer? 2 Answers

Score Counter 2 Answers

Keeping Score registering more than one Point sometimes on Destroy 2 Answers

GUI Text Score counter 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