简体   繁体   中英

How to pass score to scoreboard if they are attached to different objects and are in different scenes c#

I've read a lot of posts about this exception but none give me a working answer...I am begginer in unity and i can't solve one little problem so I hope for your help. Any useful link or something would help me. And any help is appreciated.

I have got two scenes. with two scripts attached to different objects. In on object i got score controller script in another one scoreboard script and i need to pas score to scoreboard but dont know how to do that properly and nothing works for me.

This is my scoreController script ( its attached to one game object in game scene), (it is attached to another object called "lialia" as you see in scorecontroller script)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class ScoreController : MonoBehaviour {

    public Text scoreText;

    public GameObject gameOverText;
    public bool gameOver = false;
    int score = 0;
    public  static int finall=0;


    void Update() {
        scoreText.text = score.ToString();
    }


   public void OnTriggerEnter2D(Collider2D target)
    {
        if (target.tag == "Bomb")
        {
           Time.timeScale = 0;
           gameOverText.SetActive(true);
           gameOver = true;
           finall = PlayerPrefs.GetInt("score");
           GameObject.Find("lialia").GetComponent<ScoreBoard>().CheckForHighScore(finall);

        }
    }

   public void OnTriggerExit2D(Collider2D target){

        if (target.tag == "Fruit") {
            Destroy(target.gameObject);
            score++;
           finall = score;
          PlayerPrefs.SetInt("score", finall);

        }
    }
}

This is my ScoreBoardScript ( you can only look to CheckForHIghScore(); method) which is attached to "lialia" object as you see in previous script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScoreBoard : MonoBehaviour {
    public Text[] highScores;
    int[] highScoreValues;

     void Awake()
    {
        DontDestroyOnLoad(gameObject);
    }
    void Start () {
        highScoreValues = new int[highScores.Length];
        for (int x =0; x < highScores.Length; x++) {
            highScoreValues[x] = PlayerPrefs.GetInt("highScoreValues" + x);
        }
        DrawScores();
    }
    void SaveScore() {
        for (int x =0; x < highScores.Length; x++) {
            PlayerPrefs.SetInt("highScoreValues" + x, highScoreValues[x]);
        }
    }
    public void CheckForHighScore(int finall) {

        for (int x = 0; x < highScores.Length; x++) {
            if (finall > highScoreValues[x]) {
                for (int y=highScores.Length - 1; y > x; y--){
            highScoreValues[y] = highScoreValues[y - 1];
            }
        highScoreValues[x] =finall;
        DrawScores();
        SaveScore();
                break;

            }
        }
    }
    void DrawScores() {
        for (int x = 0; x < highScores.Length; x++) {
            highScores[x].text = highScoreValues[x].ToString();
        }
    }
    // Update is called once per frame
    void Update () {

    }
}

Depends on your project structure and platform.

Call DontDestroyOnLoad(this.gameObject) in Awake() function in MonoBehaviour script to keep objects alive between scenes, but it has some bugs when is used with old android versions (below kitkat, if I'm not mistaken).

You can create singleton ScoreManager class and keep all your scores and shared data there.

You can use PlayerPrefs to save scores in one scene and load in the other when Start() function is called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM