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 maartenn-87 · Apr 12, 2012 at 02:52 PM · scoresystemhealth

Basic Question: Score System giving random numbers.

I'm quite a noob when it comes to scripting. I have this script added to some cube objects.

 var buck : int = 1;
 var target : Collider; 
 var ScoreBoard : GUIText; 
 
 
 function OnTriggerEnter(cubeTrigger : Collider)
 {
 if (cubeTrigger == target)  
    
    {
         buck -= 1;
         ScoreBoard.text = ("") + buck;
    }
 }



I've set the Buck to 3 in the inspector. Now when I hit a cube it gets -1 so it will be 2. But when I hit the next Cube it adds +2 or +3 sometimes. What am I doing wrong in this code?

Comment
Add comment · Show 1
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 fafase · Apr 23, 2012 at 12:53 PM 0
Share

You said you set Buck to 3 in the inspector, did you do it for all cubes or only one? and you should remove the () around ""

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by gregzo · Apr 12, 2012 at 02:57 PM

Try this :

ScoreBoard.text = buck.ToString();

And if I where you, I'd name ScoreBoard scoreBoard...

As for the strange variance of your buck, search all your scripts to see if it's not incremented elsewhere!

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 Owen-Reynolds · Apr 12, 2012 at 04:51 PM 0
Share

To help check if someone else is changing it, maybe add, just next to buck-=1;: Debug.Log("hit, b="+buck);. If the score changes and you don't see that line, someone else is changing it.

""+buck is a pretty common shortcut to convert to string in C# -- I assume it's the same in unityScript.

avatar image
0

Answer by Stardog · Apr 23, 2012 at 01:19 PM

You're correct about it not being the correct way to do it, because each cube will have its own "buck".

I would make a seperate "ScoreObject" with a ScoreScript attached, and have the buck/scoreboard variables in there. Also, it should "listen" for an AddToScore message, sent by the cubes, and then add or subract from the buck.

Use this messenger system. Put the files into Assets/Plugins.

Example:

ScoreScript on ScoreObject

 using UnityEngine;
 using System.Collections;
 
 public class ScoreScript : MonoBehaviour {
     
     public int buck = 1;
     
     void OnEnable() {
         Messenger<int>.AddListener("add_to_score", AddToScore);
     }
     void OnDisable() {
         Messenger<int>.RemoveListener("add_to_score", AddToScore);
     }

     void AddToScore(int scoreToAdd)
     {
         buck += scoreToAdd;
     }
 
 }

Then on each cube, it would be like what you have now, except with a "scoreToAdd variable" (this would be -1 for your example) and put this inside the "if" statement:

 Messenger<int>.Broadcast("add_to_score", scoreToAdd);
 

Then use a seperate GUIManager GameObject to display the "buck" in ScoreScript.

Basically, when a cube is hit, it will broadcast a message, and the ScoreObject will pick it up, then change the score to whatever was sent.

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

7 People are following this question.

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

Related Questions

How to create a point/score system based on player performance. 2 Answers

Main Menu issues when restart 1 Answer

Scoring System 1 Answer

Health Regen Stop After Damage 1 Answer

Calling Enemy Health Adding to Player Score 0 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