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 Xain · Sep 12, 2014 at 01:10 PM · guiscore

Showing score with GUI

Hello everyone i want to show the score when map changed here is my full script ;

 using UnityEngine;
 using System.Collections;
 
 public class Fuel : MonoBehaviour {
 
 
     public GUIStyle penguin1;
     public GUIStyle penguin2;
     public GUIStyle penguin3;
     public GUIStyle penguin4;
     public GUIStyle penguin5;
 
     public float fuel = 20;
     private int score = 0;
 
     
 
     void Update() {
         fuel -= 1 * Time.deltaTime; 
 
         
         if (fuel < 0) {
 
         }
         
         if (fuel > 40 ) {
             fuel = 40;
         }
         
         if (fuel < -2) {
             Application.LoadLevel("gameOver");
         }
         
         print (fuel);
     
     }
     
 
 
     void OnGUI () {
 
 
         if (fuel >= 30 && fuel <= 40) 
             GUI.Box (new Rect (60,18,65,45), "" , penguin1);
         
         
         if (fuel >= 20 && fuel <= 30)
             GUI.Box (new Rect (60,18,65,45), "" , penguin2);  
         
 
         if (fuel >= 10 && fuel <= 20)
             GUI.Box (new Rect (60,18,65,45), "" , penguin3);                          
         
         
         if (fuel >= 0 && fuel <= 10)
             GUI.Box (new Rect (60,18,65,45), "" , penguin4);
 
 
         if (fuel < 0)
             GUI.Box (new Rect (60,18,65,45), "" , penguin5);
 
 
 
         if (Application.loadedLevelName == "gameOver") {
 
         
         }
     }
     
     void OnTriggerEnter (Collider other) {
         fuel +=8; 
             score +=1;
     }
     
 
     void OnTriggerExit (Collider other)  {
         Destroy(other.gameObject);
         
     }
 }


When map changed to gameOver i want to show the score is it possible with gui ?

Thanks..

Comment
Add comment
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

2 Replies

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

Answer by Jeric-Miana · Sep 12, 2014 at 02:41 PM

Use this one bro, hope this helps

     void Update()
     {
      if (fuel < -2) 
     {
     Application.LoadLevel("gameOver");
     PlayerPrefs.SetInt("SaveScore",score);
     // save your current score 
     }
     }

Attach this script in your next scene

     void OnGUI()
     {
     GUI.Label (Rect (10, 10, 100, 20), "Score" + PlayerPrefs.GetInt("SaveScore"));
 //display saved score 
     }





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 Xain · Sep 12, 2014 at 03:13 PM 0
Share

Thanks for answer i did the first part but in the second part when i create a new script and paste your code it gives me error ;

"Expression denotes a type', where a variable', value' or method group' was expected."

avatar image Xain · Sep 12, 2014 at 03:31 PM 0
Share

Oh i fixed it. It's supposed to be (new Rect (10, 10, 100, 20) Thanks for answer :)

avatar image Jeric-Miana · Sep 13, 2014 at 03:35 AM 0
Share

no problem :)

avatar image
2

Answer by gnirts · Sep 12, 2014 at 03:42 PM

Hi,

I don't know how you manage the logic of your game (pre-made levels, single level with procedurally generation etc.).

In my game don't use PlayerPrefs for some reasons, so when I have to display some score I save a global var in the namespace of the project (I have a public static class called GlobalVariables), then I have my ProgrammaticGUI class which contains all general GUI info and rules.

In your score code I would put... let's say something like:


void OnTriggerEnter (Collider other) {
    GlobalVariables.Fuel +=8;
    GlobalVariables.GlobalScore +=1;
}

With a couple of booleans you manage wether to show or not the score (of your level or global score etc.)

Something like


void OnGUI()
{
    if(ShowGlobalScore){
        GUI.Label (MyRect, GlobalVariables.GlobalScoreVar);
    }
}

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

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

Related Questions

Need help displaying score (JavaScript) 1 Answer

Adding score when enemy dies (Errors) 2 Answers

Trouble displaying a variable as a GUI from a different script 1 Answer

Scoring with positions 1 Answer

Count the Score 1 Answer


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