如何在两个c#Unity scipts之间共享变量?

时间:2017-11-07 12:40:59

标签: c# unity3d

我有一个游戏,它会计算高分,硬币等......我希望游戏中的高分显示在文本字段的单独MainMenuScene上。但Unity抛出错误:Assets / Scripts / MainMenuUI.cs(13,39):错误CS0029:无法隐式转换类型int' to字符串' 如何将uiManger中的高分传递给MainMenuUI脚本。并且在离开游戏后也必须保存。

脚本是C#。

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

public class uiManager : MonoBehaviour
{

    public Image[] imageBg;
    public Button[] buttons;
    public Text scoreText;
    bool gameOver;
    int score;
    public Text highScore;

    // Use this for initialization
    void Start()
    {
        gameOver = false;
        score = 0;
        InvokeRepeating("scoreUpdate", 3.0f, 0.5f);
        highScore.text = PlayerPrefs.GetInt("", 0).ToString();
    }

    // Update is called once per frame
    void Update()
    {
        scoreText.text = score.ToString();

        if (score > PlayerPrefs.GetInt("", 0))
        {
            PlayerPrefs.SetInt("", score);
            highScore.text = score.ToString();
        }

    }

    void scoreUpdate()
    {
        if (gameOver == false)
        {
            score += 1;
        }
    }

    public void gameOverActivated()
    {
        gameOver = true;

        foreach (Image image in imageBg)
        {
            image.gameObject.SetActive(true);
        }


        foreach (Button button in buttons)
        {
            button.gameObject.SetActive(true);
        }



    }

    public void Replay()
    {

        Application.LoadLevel(Application.loadedLevel);

    }

    public void mainMenu()
    {

        Application.LoadLevel("MainMenu");
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MainMenuUI : MonoBehaviour {

    public Text highestScore;

    // Use this for initialization
    void Start () {
        uiManager uiMngr = GetComponent<uiManager>();
        uiMngr.highScore.text = PlayerPrefs.GetInt("", 0).ToString();
    }

    // Update is called once per frame
    void Update () {

    }
}

0 个答案:

没有答案
相关问题