Score and Highscore help

Can anyone help me ? I’m trying to make a score and highscore system … but I really can’t do this … Idk why but nothing works … my game has 2 game modes … arcade and time trial … Can any1 please do this for me ? Here are my scripts …

using UnityEngine;
using System.Collections;

//two scripts are made of the same kind with little changes, this one used in arcade mode scene and other textinstantiate1 is used in timetrial scene


public class textinstantiate : MonoBehaviour {

    //a hell lot of prefabs are created for creating the illusion
    //which are drag droped in this public text gameobject
    public GameObject[] text;

    //a copy of each instantiated text object is added in this clone gameobject
    GameObject[] clone;

    //this one is useless, if you wanna use the OnGUI for touch buttons, a commented code is added at the bottom
    public GUIStyle[] styles;

    //just variables to for string comparision
    string textcolor="q";
    string color="p";
    string c="z";
    string txt="x";

    //public score guitext drag-n-dropped here
    public GUIText score;

    //for scoring
    int count=0;

    //pointer objects(blinking indicators on screen)
    public GameObject w1, w2;

    //colors and text guitexture are grag-dropped here from the "game" named object in hierarchy
    public GUITexture[] colors;
    //colors guitexture from "common" gameobject are added here
    public GUITexture[] common;

    bool flag=false;

    //button animations
    public Animation[] anim;

    //other gameobject from hierarchy
    public GameObject gameover, game, commongui;

    //gameover ui components
    public GameObject[] gameovergui;
    public Sprite[] gameovertouch;
    public Sprite[] tempgameover;

    void Awake()
    {
        Time.timeScale=1;
        PlayerPrefs.SetInt("timetrial", 0);
        PlayerPrefs.SetInt("gameover", 0);
        PlayerPrefs.Save();
        foreach(Animation a in anim)
        {
            a.wrapMode=WrapMode.Once;
        }

        //if not level 3 "common" gameobject is set to active else "game" gameobject is set to active from hierarchy
        if(PlayerPrefs.GetInt("level")!=3)
        {
            commongui.SetActive(true);
        }
        else
            game.SetActive(true);
    }
   
    // Use this for initialization
    void Start ()
    {

        //instantiation of texts from invokerepeating function
        if(PlayerPrefs.GetInt("level")==3)
        {
            InvokeRepeating("create", 0, 2f);
        }
        else
            InvokeRepeating("create", 0, 1f);


        //blinking of pointers through invokerepeating
        InvokeRepeating("fun", 0, 1f);
    }

    void fun()
    {
        if(flag)
        {
            w1.SetActive(false);
            w2.SetActive(false);
            flag=false;
        }
        else
        {
            w1.SetActive(true);
            w2.SetActive(true);
            flag=true;
        }
    }

    void create()
    {
        Instantiate(text[Random.Range(0, text.Length)], new Vector3(0, 4, 0), Quaternion.identity);
        clone=GameObject.FindGameObjectsWithTag("text");
       
        foreach(GameObject g in clone)
        {
            g.transform.position=new Vector2(0, g.transform.position.y-2f);
        }
    }


    //this function recives the values from "textcontrol script" via sendmessage function
    void pickme(string str1)
    {
        if(PlayerPrefs.GetInt("level")==3)
        {
            textcolor=str1[0].ToString();
            color=str1[1].ToString();
            //Debug.Log(textcolor+"textcolor");
            //Debug.Log(color+"color");
        }
        else
            textcolor=str1;
    }

    //destroying the instantiated text objects when buttons(color or text buttons from scene) are pressed at the right time
    void remove()
    {
        clone=GameObject.FindGameObjectsWithTag("text");
        foreach(GameObject g in clone)
        {
            if(g.transform.position.y==-2f)
            {
                count++;
                Destroy(g);
            }
        }
    }

    void Update()
    {

        if(PlayerPrefs.GetInt("pause")==0)
        {
            Time.timeScale=1;
        }

        //here you can see the implementation of touch control via two different methods...using input.touch and from input.mousebuttondown...
        //each button in hierarchy represents a its color name or text name...which are assigned in the variables when touched
        score.text=count+"";
        if(PlayerPrefs.GetInt("level")==3)
        {
        if (Input.touchCount > 0)
        {
            for(int i = 0; i < Input.touchCount; i++)
                {
                Touch t = Input.GetTouch(i);
               
                if (t.phase == TouchPhase.Began) {
                    if (colors[0].HitTest(t.position, Camera.main)) {
                            audio.Play();
                            c="b";
                            anim[0].Play();
                    }
                   
                    if (colors[1].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        c="c";
                            anim[1].Play();
                    }

                    if (colors[2].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        c="g";
                            anim[2].Play();
                    }


                    if (colors[3].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        c="m";
                            anim[3].Play();
                    }

                    if (colors[4].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        c="o";
                            anim[4].Play();
                    }

                    if (colors[5].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        c="r";
                            anim[5].Play();
                    }

                    if (colors[6].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        c="v";
                            anim[6].Play();
                    }

                    if (colors[7].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        c="y";
                            anim[7].Play();
                    }
                    if (colors[8].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        txt="b";
                            anim[8].Play();
                    }
                   
                    if (colors[9].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        txt="c";
                            anim[9].Play();
                    }
                   
                    if (colors[10].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        txt="g";
                            anim[10].Play();
                    }
                   
                   
                    if (colors[11].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        txt="m";
                            anim[11].Play();
                    }
                   
                    if (colors[12].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        txt="o";
                            anim[12].Play();
                    }
                   
                    if (colors[13].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        txt="r";
                            anim[13].Play();
                    }
                   
                    if (colors[14].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        txt="v";
                            anim[14].Play();
                    }
                   
                    if (colors[15].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        txt="y";
                            anim[15].Play();
                    }
                }
                if (t.phase == TouchPhase.Ended) {
                    // Stop all movement
                    Debug.Log("touch ended");
                    c="a";
                }
            }
        }
        }

        if(Input.GetMouseButtonDown(0))
        {

            Vector2 pos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
            RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(pos), Vector2.zero);


            if (common[0].HitTest(Input.mousePosition, Camera.main)) {
                audio.Play();
                txt="b";
                anim[16].Play();
            }
            if (common[1].HitTest(Input.mousePosition, Camera.main)) {
                audio.Play();
                txt="c";
                anim[17].Play();
            }
            if (common[2].HitTest(Input.mousePosition, Camera.main)) {
                audio.Play();
                txt="g";
                anim[18].Play();
            }
            if (common[3].HitTest(Input.mousePosition, Camera.main)) {
                audio.Play();
                txt="m";
                anim[19].Play();
            }
            if (common[4].HitTest(Input.mousePosition, Camera.main)) {
                audio.Play();
                txt="o";
                anim[20].Play();
            }
            if (common[5].HitTest(Input.mousePosition, Camera.main)) {
                audio.Play();
                txt="r";
                anim[21].Play();
            }
            if (common[6].HitTest(Input.mousePosition, Camera.main)) {
                audio.Play();
                txt="v";
                anim[22].Play();
            }
            if (common[7].HitTest(Input.mousePosition, Camera.main)) {
                audio.Play();
                txt="y";
                anim[23].Play();
            }

            if (hit.collider.name=="home") {
                audio.Play();
                Application.LoadLevel("menu");
                hit.collider.gameObject.GetComponent<SpriteRenderer>().sprite=gameovertouch[0];
            }
            if (hit.collider.name=="restart") {
                audio.Play();
                Application.LoadLevel(Application.loadedLevel);
                hit.collider.gameObject.GetComponent<SpriteRenderer>().sprite=gameovertouch[1];
            }
        }
       
        if(Input.GetMouseButtonUp(0))
        {
            txt="a";
            gameovergui[0].GetComponent<SpriteRenderer>().sprite=tempgameover[0];
            gameovergui[1].GetComponent<SpriteRenderer>().sprite=tempgameover[1];
        }
       
//        if(PlayerPrefs.GetInt("pause")==1)
//        {
//            //pause.transform.position=Vector2.Lerp(pause.transform.position, new Vector2(0, 0), Time.deltaTime*10);
//            game.transform.position=Vector2.Lerp(game.transform.position, new Vector2(-20f, 0), Time.deltaTime*10);
//            commongui.transform.position=Vector2.Lerp(game.transform.position, new Vector2(-20f, 0), Time.deltaTime*10);
//        }
//        if(PlayerPrefs.GetInt("pause")==0)
//        {
//            //pause.transform.position=Vector2.Lerp(pause.transform.position, new Vector2(1.5f, 0), Time.deltaTime*10);
//            game.transform.position=Vector2.Lerp(game.transform.position, new Vector2(0, 0), Time.deltaTime*10);
//            commongui.transform.position=Vector2.Lerp(game.transform.position, new Vector2(0, 0), Time.deltaTime*10);
//        }
       
        if(PlayerPrefs.GetInt("gameover")==1)
        {
            gameover.transform.position=Vector2.Lerp(gameover.transform.position, new Vector2(0, 0), Time.deltaTime*10);
            game.transform.position=Vector2.Lerp(game.transform.position, new Vector2(20f, 0), Time.deltaTime*10);
            commongui.transform.position=Vector2.Lerp(game.transform.position, new Vector2(20f, 0), Time.deltaTime*10);

        }

//        if(PlayerPrefs.GetInt("gameover")==1 && !flag)
//        {
//            PlayerPrefs.SetInt("ad", PlayerPrefs.GetInt("ad")+1);
//            PlayerPrefs.Save();
//            if(PlayerPrefs.GetInt("ad")>2)
//            {
//                GoogleMobileAd.StartInterstitialAd();
//                PlayerPrefs.SetInt("ad", 0);
//                PlayerPrefs.Save();
//            }
//            Debug.Log(PlayerPrefs.GetInt("ad").ToString());
//            flag=true;
//        }
       
    }



    //main logic of the game, simple comparision of the strings are done to check the incoming instantiated text objects when they reach the blinking pointer
    //this way it is checked if button color pressed and the instantiated text's color are same // this game is all illusion, i would recommend you to play it several times before going through scripts to understand it.
    //even you dont learn the game u can learn touch controls and menu transitions from this
    void FixedUpdate()
    {
        if(PlayerPrefs.GetInt("level")==3)
        {
            if(color.Equals(c) && textcolor.Equals(txt))
            {
                remove();
            }
        }
        else
        {
            if(textcolor.Equals(txt))
            {
                remove();
            }
        }
       
    }
   
}

and …

using UnityEngine;
using System.Collections;


// this script is similar to the "textinstantiate" script only have a little changes...
//everything you need know is decribed in another script
public class textinstantiate1 : MonoBehaviour {

    public GameObject[] text;
    GameObject[] clone;
    public GUIStyle[] styles;
    string textcolor="p";
    string color="q";
    public GUIText score, gtime;
    int count=0;
    public GameObject w1, w2;
    bool flag=false;
    string c="s";
    string txt="t";
    public GUITexture[] colors;
    public Animation[] anim;
    float time=31;
    public GameObject gameover, pause, game, commongui;
    public GameObject[] gameovergui;
    public Sprite[] gameovertouch;
    public Sprite[] tempgameover;
    public GameObject[] pausegui;
    public Sprite[] pausetouch;
    public Sprite[] temppause;
    public GUITexture[] common;
    void Awake()
    {
        Time.timeScale=1;
       
        PlayerPrefs.SetInt("ad", PlayerPrefs.GetInt("ad")+1);
        PlayerPrefs.SetInt("timetrial", 1);
        PlayerPrefs.SetInt("gameover", 0);
        PlayerPrefs.SetInt("pause", 2);
        PlayerPrefs.Save();
        foreach(Animation a in anim)
        {
            a.wrapMode=WrapMode.Once;
        }
        if(PlayerPrefs.GetInt("level")!=3)
        {
            commongui.SetActive(true);
        }
        else
            game.SetActive(true);
    }
   
    // Use this for initialization
    void Start ()
    {
        if(PlayerPrefs.GetInt("level")==3)
        {
            InvokeRepeating("create", 0, 2f);
        }
        else
            InvokeRepeating("create", 0, 1f);
        InvokeRepeating("fun", 0, 1f);
    }

    void fun()
    {
        if(flag)
        {
            w1.SetActive(false);
            w2.SetActive(false);
            flag=false;
        }
        else
        {
            w1.SetActive(true);
            w2.SetActive(true);
            flag=true;
        }
        if(PlayerPrefs.GetInt("gameover")!=1)
        {
            time--;
            gtime.text=time.ToString();
            Debug.Log(time.ToString());
        }


    }

    void create()
    {
        Instantiate(text[Random.Range(0, text.Length)], new Vector3(0, 4, 0), Quaternion.identity);
        clone=GameObject.FindGameObjectsWithTag("text");
       
        foreach(GameObject g in clone)
        {
            g.transform.position=new Vector2(0, g.transform.position.y-2f);
        }
    }

    void pickme(string str1)
    {
        if(PlayerPrefs.GetInt("level")==3)
        {
            textcolor=str1[0].ToString();
            color=str1[1].ToString();
        }
        else
            textcolor=str1;
    }

    void remove()
    {
        clone=GameObject.FindGameObjectsWithTag("text");
        foreach(GameObject g in clone)
        {
            if(g.transform.position.y==-2f)
            {
                count++;
                Destroy(g);
            }
        }
    }

    void Update()
    {

        if(PlayerPrefs.GetInt("pause")==0)
        {
            Time.timeScale=1;
        }

        score.text=count+"";
        if(PlayerPrefs.GetInt("level")==3)
        {
        if (Input.touchCount > 0)
        {
            for(int i = 0; i < Input.touchCount; i++)
                {
                Touch t = Input.GetTouch(i);
               
                if (t.phase == TouchPhase.Began) {
                        audio.Play();
                    if (colors[0].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        c="b";
                            anim[0].Play();
                    }
                   
                    if (colors[1].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        c="c";
                            anim[1].Play();
                    }

                    if (colors[2].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        c="g";
                            anim[2].Play();
                    }


                    if (colors[3].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        c="m";
                            anim[3].Play();
                    }

                    if (colors[4].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        c="o";
                            anim[4].Play();
                    }

                    if (colors[5].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        c="r";
                            anim[5].Play();
                    }

                    if (colors[6].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        c="v";
                            anim[6].Play();
                    }

                    if (colors[7].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        c="y";
                            anim[7].Play();
                    }
                    if (colors[8].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        txt="b";
                            anim[8].Play();
                    }
                   
                    if (colors[9].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        txt="c";
                            anim[9].Play();
                    }
                   
                    if (colors[10].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        txt="g";
                            anim[10].Play();
                    }
                   
                   
                    if (colors[11].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        txt="m";
                            anim[11].Play();
                    }
                   
                    if (colors[12].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        txt="o";
                            anim[12].Play();
                    }
                   
                    if (colors[13].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        txt="r";
                            anim[13].Play();
                    }
                   
                    if (colors[14].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        txt="v";
                            anim[14].Play();
                    }
                   
                    if (colors[15].HitTest(t.position, Camera.main)) {
                            audio.Play();
                        txt="y";
                            anim[15].Play();
                    }
                }
                if (t.phase == TouchPhase.Ended) {
                    // Stop all movement
                    Debug.Log("touch ended");
                    c="a";
                }
            }
        }
        }


        if(Input.GetMouseButtonDown(0))
        {

            Vector2 pos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
            RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(pos), Vector2.zero);


            if (common[0].HitTest(Input.mousePosition, Camera.main)) {
                audio.Play();
                txt="b";
                anim[16].Play();
            }
            if (common[1].HitTest(Input.mousePosition, Camera.main)) {
                audio.Play();
                txt="c";
                anim[17].Play();
            }
            if (common[2].HitTest(Input.mousePosition, Camera.main)) {
                audio.Play();
                txt="g";
                anim[18].Play();
            }
            if (common[3].HitTest(Input.mousePosition, Camera.main)) {
                audio.Play();
                txt="m";
                anim[19].Play();
            }
            if (common[4].HitTest(Input.mousePosition, Camera.main)) {
                audio.Play();
                txt="o";
                anim[20].Play();
            }
            if (common[5].HitTest(Input.mousePosition, Camera.main)) {
                audio.Play();
                txt="r";
                anim[21].Play();
            }
            if (common[6].HitTest(Input.mousePosition, Camera.main)) {
                audio.Play();
                txt="v";
                anim[22].Play();
            }
            if (common[7].HitTest(Input.mousePosition, Camera.main)) {
                audio.Play();
                txt="y";
                anim[23].Play();
            }

            if(time>1)
            {
                if(hit.collider.name=="pausebg")
                {
                    Invoke("farzi", .5f);
                    PlayerPrefs.SetInt("pause", 1);
                    PlayerPrefs.Save();
                }
            }

            if (hit.collider.name=="home") {
                audio.Play();
                Application.LoadLevel("menu");
                hit.collider.gameObject.GetComponent<SpriteRenderer>().sprite=gameovertouch[0];
            }
            if (hit.collider.name=="restart") {
                audio.Play();
                Application.LoadLevel(Application.loadedLevel);
                hit.collider.gameObject.GetComponent<SpriteRenderer>().sprite=gameovertouch[1];
            }
            if(hit.collider.name=="resume_p")
            {
                audio.Play();
                PlayerPrefs.SetInt("pause", 0);
                PlayerPrefs.SetInt("ad", PlayerPrefs.GetInt("ad")+1);
                PlayerPrefs.Save();
                hit.collider.gameObject.GetComponent<SpriteRenderer>().sprite=pausetouch[0];
            }
            if(hit.collider.name=="restart_p")
            {
                audio.Play();
                Application.LoadLevel(Application.loadedLevel);
                hit.collider.gameObject.GetComponent<SpriteRenderer>().sprite=pausetouch[1];
            }
            if(hit.collider.name=="home_p")
            {
                audio.Play();
                Application.LoadLevel("menu");
                hit.collider.gameObject.GetComponent<SpriteRenderer>().sprite=pausetouch[2];
            }
        }

        if(Input.GetMouseButtonUp(0))
        {
            txt="a";
            gameovergui[0].GetComponent<SpriteRenderer>().sprite=tempgameover[0];
            gameovergui[1].GetComponent<SpriteRenderer>().sprite=tempgameover[1];
            pausegui[0].GetComponent<SpriteRenderer>().sprite=temppause[0];
            pausegui[1].GetComponent<SpriteRenderer>().sprite=temppause[1];
            pausegui[2].GetComponent<SpriteRenderer>().sprite=temppause[2];
        }

        if(PlayerPrefs.GetInt("pause")==1)
        {
            pause.transform.position=Vector2.Lerp(pause.transform.position, new Vector2(0, 0), Time.deltaTime*10);
            game.transform.position=Vector2.Lerp(game.transform.position, new Vector2(-20f, 0), Time.deltaTime*10);
            commongui.transform.position=Vector2.Lerp(game.transform.position, new Vector2(-20f, 0), Time.deltaTime*10);
        }
        if(PlayerPrefs.GetInt("pause")==0)
        {
            pause.transform.position=Vector2.Lerp(pause.transform.position, new Vector2(20f, 0), Time.deltaTime*10);
            game.transform.position=Vector2.Lerp(game.transform.position, new Vector2(0, 0), Time.deltaTime*10);
            commongui.transform.position=Vector2.Lerp(game.transform.position, new Vector2(0, 0), Time.deltaTime*10);
        }

        if(time==0)
        {

            gameover.transform.position=Vector2.Lerp(gameover.transform.position, new Vector2(0, 0), Time.deltaTime*10);
            game.transform.position=Vector2.Lerp(game.transform.position, new Vector2(20f, 0), Time.deltaTime*10);
            commongui.transform.position=Vector2.Lerp(game.transform.position, new Vector2(20f, 0), Time.deltaTime*10);
            //Invoke("farzi2", 1);
            PlayerPrefs.SetInt("gameover", 1);
        }


    }

    //farzi is hindi word...means useless but this one is used
    void farzi()
    {
        Time.timeScale=0;
    }

    void FixedUpdate()
    {
        if(PlayerPrefs.GetInt("level")==3)
        {
            if(color.Equals(c) && textcolor.Equals(txt))
            {
                remove();
            }
        }
        else
        {
            if(textcolor.Equals(txt))
            {
                remove();
            }
        }

    }

}

what’s your game, gameplay, game mechanics? how and when does the player get points for doing what?

Here is the game … i downloaded it … and i’m trying to implement a highscore system … but it don’t work …

Any1 ?

No one can help me ?

It’s kind of bad etiquette to just outright ask someone to do something for you. And kind of lazy. And you’ve just dumped a couple of scripts on us without any explanation. And you haven’t answered the questions another user asked you…

Try to be a bit more specific. What exactly have you tried? What exactly are you having trouble with?

No one in their right mind will bother to scour through 800 lines of code to try to figure what your issue is.

1 Like

downloaded it will have a look when i have time…
btw the scripts you posted are giving me a slight headache :wink:

Me too …

If you have a game with a score and want to add a high score mechanism, you essentially need these parts, and you need to put them into the game in the appropriate spots:

  1. a storage mechanism to store the high score. I recommend PlayerPrefs.GetInt() and .SetInt()

  2. a display mechanism to show the high score on your front menu page (and maybe your in-game screen too)

  3. a comparison statement that is called after each game to compare the last score with the high score, and if it is higher, then update the storage mechanism in #1.

Work on each part separately, and test it by debugging to make sure it works as expected.

1 Like

hey Louis LCM,
I had a look at you’re game and i’m sorry to say that it is just too much spaghetti code, it would take too much time and energy just to figure out what’s happening as opposed to rewriting your game.
It’s clear you have reached your limit in what you can code without thinking Object Oriented, my advice is to check out some basic C# tutorials on youtube and within a week or so make that same game again.
everything will be so much clearer on how to handle your code and build your game…

1 Like

Thank you anyway :smile: !