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 /
This question was closed Nov 25, 2013 at 04:56 AM by clunk47 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by sid30rs · Oct 12, 2013 at 08:09 AM · systemhighscoresscores

In-game high score system

I am trying to make a high score system for each level.

I want to display high scores of level 1 in level 1 and high scores of level 2 in level 2.

Any help!?

This is my manager script which contains scores:

 var TotalShips : int;
 var ShipsKilled : int;
 var enemeyShip : Transform;
 var playerShip : Transform;
 var playerLives : int = 2;
 var player : GameObject;
 var level : int = 1;
 var score : int = 0;
 var playerlive : int = 0;
 var waitTime : float = 1.8;
 var showlabel : boolean = true;
 
 var gameover : boolean = false;
 
 var mystyle : GUIStyle;
 
 function Start () {
     InvokeRepeating("Spawn", waitTime, waitTime);
 }
 
 function Update () {
         
 }
 
 function ShipCounter(){
     if(TotalShips % 10 == 0){
         level++;
     }
     return (level);
 }
 
 function Spawn(){
     if(player == null && playerLives >= 1){
         playerLives--;
         Instantiate(playerShip);
         player = GameObject.FindGameObjectWithTag("Player");
         }
     if(playerLives == 0){
         gameover = true;
         gameObject.FindGameObjectWithTag("Player").renderer.enabled = false;
     }
 }
 
 function OnGUI(){
         GUI.Label(Rect(0, 0, 100, 100), "" + score, mystyle);
 }
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

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by vexe · Oct 12, 2013 at 08:20 AM

You could create a class for your levels, and have each level own its score value.

[C#]

 public class Level
 {
    public int Score { get; set; }
    // other stuff if you want...
 }

And make a LevelManager while you're at it:

 public class LevelManager
 {
    public Level[] Levels { private set; get; }
    public Level CurrentLevel { private set; get; }
    private int index;
    public LevelManager(int nLevels)
    {
        Levels = new Level[nLevels];
        index = 0;
        CurrentLevel = Levels[0];
    }
 
    public void NextLevel()
    {
       index = (index + 1) % Levels.Length;
       CurrentLevel = Levels[index];
    }
 }

In your code:

 private LevelManager levelManager;
 public int nMaxLevels;
 
 void Awake() // or Start
 {
    levelManager = new LevelManager(nMaxLevels);
 }

Now whenever you need to increment the level, just do levelManager.NextLevel();

And, if you need to get the score of the current level: levelManager.CurrentLevel.Score;

The key thing to keep in mind, is to keep your classes have a single responsibility.

How about it?

EDIT:

[JS]

Level.js

 var Score : int;
 // other stuff if you want...

LevelManager.js

 var Levels : Level[];
 var CurrentLevel : Level;
 private var index : int;

 function LevelManager(nLevels : int)
 {
    Levels = new Level[nLevels];
    index = 0;
    CurrentLevel = Levels[0];
 }
 
 function NextLevel()
 {
   index = (index + 1) % Levels.Length;
   CurrentLevel = Levels[index];
 }

You don't need to attach Level nor LevelManager to anything.

In your code:

 var levelManager : LevelManager;
 var nMaxLevels : int;
 
 function Awake() // or Start
 {
    levelManager = new LevelManager(nMaxLevels);
 }

You'd access them the same way I described in the C# part ^.

Comment
Add comment · Show 7 · 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 vexe · Oct 12, 2013 at 08:23 AM 0
Share

@multigold99: Can you handle the C#->JS conversion, or do you want me to do it for you?

avatar image sid30rs · Oct 12, 2013 at 08:30 AM 0
Share

I will be need Javascript if you can

avatar image sid30rs · Oct 12, 2013 at 08:32 AM 0
Share

Btw I just updated it with this code But its not saving the score :'(

function OnGUI(){ GUI.Label(Rect(0, 0, 100, 100), "" + score, mystyle);

     PlayerPrefs.SetInt('highscore', score);
     PlayerPrefs.Save();
     if(PlayerPrefs.Has$$anonymous$$ey('highscore')){
         print(PlayerPrefs.GetInt('highscore'));
         GUI.Label(Rect(0, 0, 100, 100), "" + PlayerPrefs.GetInt('highscore') , mystyle1);
     }

}

avatar image vexe · Oct 12, 2013 at 08:33 AM 0
Share

O$$anonymous$$ I'm not a JS coder, and from what I know, in Unity when you create a JS script, JS automatically inherits from $$anonymous$$onoBehaviour. In your case, you just need a simple Level and a Level$$anonymous$$anager, they don't need to be $$anonymous$$onoBehaviours. But anyway, I'm gonna translate for you.

avatar image vexe · Oct 12, 2013 at 08:40 AM 0
Share

Updated, translated to JS. Let me know if you get any trouble.

Show more comments

Follow this Question

Answers Answers and Comments

16 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

Related Questions

how to creat and scoring and high score system for an endless runner,how do I display my score on a game over scene and also create and high score system for the same ? 0 Answers

How to update high scores using PlayerPrefs? 1 Answer

Easy online highscore system 0 Answers

Accessing local system ( File Browser ) 2 Answers

Facebook API: How to get all app users scores 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