繁体   English   中英

如果将得分附加到不同的对象并且位于不同的场景中,如何将得分传递到记分板C#

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

我已经阅读了很多有关此异常的文章,但没有一个能给我一个有效的答案...我是团结的乞little,我无法解决一个小问题,希望能对您有所帮助。 任何有用的链接或其他东西都会对我有所帮助。 感谢您的帮助。

我有两个场景。 两个脚本附加到不同的对象。 在对象上,我在另一个计分板脚本中获得了计分控制器脚本,我需要将计分器移至计分板,但不知道如何正确执行此操作,因此对我无济于事。

这是我的scoreController脚本(它附着到游戏场景中的一个游戏对象上),(它附着到另一个在得分控制器脚本中看到的名为“ lialia”的对象上)

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);

        }
    }
}

这是我的ScoreBoardScript(您只能查看CheckForHIghScore();方法),就像在上一个脚本中看到的那样,它附加到“ lialia”对象上

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 () {

    }
}

取决于您的项目结构和平台。

在MonoBehaviour脚本的Awake()函数中调用DontDestroyOnLoad(this.gameObject) ,以使场景之间的对象保持活动状态,但是当与旧的android版本一起使用时(在kitkat以下,如果我没记错的话)会出现一些错误。

您可以创建单例ScoreManager类,并将所有分数和共享数据保留在那里。

调用Start()函数时,可以使用PlayerPrefs将乐谱保存在一个场景中,并在另一个场景中加载。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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