• 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 JokerMartini · Jul 25, 2012 at 08:21 PM · counterpointsstats

Score/Points counter #Javascript

I've gone ahead and made a public function which would control the adding or subtracting of the score based on what object is hit. My only concern is how do I place that into a GUI so it updates the text when the scoreManipulator is run? These are the lines of code I've come up with but the value does not update nor does it work. The convert to string bugs out. Hope this makes sense. Thanks everyone for the help.

I have these placed on an empty object in the scene. But how do I call the function then to run?

 static var currentScore : int = 0
 
 public function scoreManipulator(p : int)
 {
  currentScore += p;
  Debug.Log(currentScore);
 }
 
 function OnGUI () {
     currentScore .ToString(); // convert the variable scoreNumber which is an integer to a string
     var centeredStyle = GUI.skin.GetStyle("Label");
     centeredStyle.alignment = TextAnchor.UpperRight;
     GUI.Label (Rect (Screen.width - 110,10,100,50),  currentScore  , centeredStyle);
 }
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 Muuskii · Jul 25, 2012 at 09:30 PM 0
Share

You could use a singleton with functions like reset score, addscore, report score, etc.

avatar image JokerMartini · Jul 25, 2012 at 10:45 PM 0
Share

Signleton? Why use that ins$$anonymous$$d of what I'm trying to do? I'm unfamiliar with the singleton method.

1 Reply

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

Answer by Seth-Bergman · Jul 25, 2012 at 10:47 PM

This is how I do it:

 static int level;
 static int goal;
 GUISkin statusInfoSkin;
 
 etc
 
     function OnGUI () {
     
              GUI.skin = statusInfoSkin;
             GUI.Box (Rect (10,10,110,100), "level:" + level);
      
      GUI.Box (Rect (10,60,180,100), "goal:" + counter + " out of " + goal);
     }

this is how I usually do it, I guess it should work with Label too (example from a current project)

Note: I'm using javascript, tried to convert but forgive me if I missed something (not sure if I need "new" Rect...)

Comment
Add comment · Show 10 · 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 Moomooz · Jul 25, 2012 at 10:52 PM 0
Share

IIm I on the right track if I add this to the Unity Car Tutorial as a checkpoint/lap system ?,or am I barking up the wrong tree lol ?

avatar image Seth-Bergman · Jul 25, 2012 at 11:15 PM 0
Share

Should work fine, you just need to access the appropriate vars and they'll show up.. I did something similar myself with the car tutorial.. just add a trigger collider at the lap/finish line to count laps

avatar image JokerMartini · Jul 25, 2012 at 11:34 PM 0
Share

How do you convert your int to strings? What are you then using to increase the values?

avatar image Seth-Bergman · Jul 25, 2012 at 11:39 PM 0
Share

I don't convert them to strings, it's not necessary.. my values are changed via other scripts in this case, but your method is perfectly sound, just call that with a value:

score$$anonymous$$anipulator(5);

and it would work.

or you could access the static var directly from any script:

ScoreScriptName.currentScore += 5;

(I guess the Strings are automatically typecast/concatenated, never thought about it)

if you want to call the method from another script, you can use

Send$$anonymous$$essage("ScoreScriptName",5);

avatar image JokerMartini · Jul 25, 2012 at 11:58 PM 0
Share

I'm getting an error when I do Send$$anonymous$$essage("ScoreScriptName",5); Its saying there is no receiver?

I did however get this method to work.. ScoreScriptName.currentScore += 5;

But I'd like to know and understand why the first one was bugging out.

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Adding score to existing code 3 Answers

Scoring (i'm stuck) 3 Answers

Highscore Save 0 Answers

GUI Help, timer and counter 1 Answer

How to give a bonus every 1000 points? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges