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 FatalNightmare · Dec 16, 2012 at 03:47 AM · guiupdatesystempoint

Point System. GUI Help.

Hello, So I have this "coin" and it floats. When Player picks up this coin, PlayerScore += 1. This all updates but I would like to have a GUI Text update. So that when the player picks up this "coin" it updates + 1. *I would like the GUIText named collectablewall01. Here is my code:

var PlayerScore : int; var ScoreText = "Score: 0"; var MaxPoints : int;

function OnTriggerEnter(other : Collider){ if(other.tag == "Point"){ PlayerScore += 1; ScoreText = "Score: " + PlayerScore; Destroy(other.gameObject); } }

function Update(){ if(PlayerScore > MaxPoints){ PlayerScore = 0; print("MaxReached"); ScoreText = "Score: 0"; } } The code is on the Player. Please help, thank you,

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 clunk47 · Dec 16, 2012 at 04:22 AM 0
Share

Just add an OnGUI function. I've predefined a Rectangle which is (xPosition, yPosition, width, height). I put the Rect default for your scoreText as (0, 0, 256, 128) this will put it 0 at x, 0 at y, 256 width max, and 128 height for the rectangle the text will appear in. You don't actually see a rectangle, this is just the area, which you can adjust in the inspector. I've added an option GUIStyle, where as you can adjust color, style, font size, change font, etc.. in the inspector. If you want to change the color in the inspector, remove the style.normal.textColor line from the Start() function. Simply replace your code with this:

     static var PlayerScore : int;
     var ScoreText = "Score: 0";
     var $$anonymous$$axPoints : int;
     var ScoreTextPosition : Rect = Rect(0, 0, 256, 128);
     var style : GUIStyle;
     
     function Start()
     {
         style.normal.textColor = Color.white;
     }
     
     function OnTriggerEnter(other : Collider)
     {
         if(other.tag == "Point")
         {
             PlayerScore += 1;
             ScoreText = "Score: " + PlayerScore;
             Destroy(other.gameObject);
         }
     }
     
     function Update()
     {
         if(PlayerScore > $$anonymous$$axPoints)
         {
             PlayerScore = 0;
             print("$$anonymous$$axReached");
             ScoreText = "Score: 0";
         }
     }
     
     function OnGUI()
     {
         GUI.Label(ScoreTextPosition, ScoreText, style);
     }
 
 
avatar image clunk47 clunk47 · Dec 16, 2012 at 04:24 AM 0
Share

Note: Simply use this script the same way you did your old text. There is no need to attach a GUIText component from the menu. OnGUI function will update automatically with the score.

1 Reply

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

Answer by aldonaletto · Dec 16, 2012 at 04:19 AM

You should place a reference to the GUIText object in this script, and assign it in the Inspector, then in your script refresh the GUIText in Update:

 var PlayerScore : int;
 var ScoreText = "Score: 0";
 var MaxPoints : int;
 var gText : GUIText; // drag here the GUIText from Hierarchy
 
 function OnTriggerEnter(other : Collider){
     if(other.tag == "Point"){
         PlayerScore += 1;
         ScoreText = "Score: " + PlayerScore;
         Destroy(other.gameObject);
     }
 }
 
 function Update(){
     if(PlayerScore > MaxPoints){
         PlayerScore = 0;
         print("MaxReached");
         ScoreText = "Score: 0";
     }
     gText.text = ScoreText; // update GUIText each frame
 }
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 FatalNightmare · Dec 16, 2012 at 04:50 AM 0
Share

Worked beautifully, thank you so much.

avatar image himanshu619 · Dec 16, 2012 at 09:02 AM 0
Share

But when the next level is loaded the score-board will toggle and starts from "0". Can you tell how it would continue adding points for next levels too?

$$anonymous$$y script is:

var score = 0; var scoreText = "Score: 0"; var mySkin : GUISkin;

function OnTriggerEnter(other : Collider ) {

if (other.tag == "Pickup") { //Pickup is the tag please replace it with your pick-able item tag

     Debug.Log("Other object is a coin");

     score += 1;         //You can replace 1 with any integer this is you score 

     scoreText = "Score: " + score;

     Debug.Log("Score is now " + score);

     Destroy(other.gameObject);

 }

}

function OnGUI () {

 GUI.skin = mySkin;

 GUI.Label (Rect (200, 10, 200, 400), scoreText.ToString()); 

}

avatar image aldonaletto · Dec 16, 2012 at 11:36 AM 0
Share

If your game has one only score variable, declare it as static: static variables are created when the game starts and survive to level loadings:

 static var score = 0;
 ...

The initialization of score is done only once, when the variable is created, thus it will keep its value across all levels.
NOTE: A static variable is unique - if there are several instances of the script where it's declared, all of them refer to the same variable. Using a static variable for the enemy health, for instance, is a disaster: when one dies, all of them drop dead at the same time, because the health value is the same!

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

Multiple Cars not working 1 Answer

Is it possible to link character skill lists to a GUI, and if so, how? 3 Answers

Help! Why doesn't this work? GUI/Static vars Help! Javascript 1 Answer

Creation of checkbox next to text 1 Answer

A big problem with Button, Please Help 2 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