1

我正在使用 playerprefs 在我的场景“标准游戏”中管理我的 HighScore

我需要在我的另一个脚本“SaveTest”中访问我的变量 HighScore,该脚本位于另一个名为“主菜单”的场景中,因此当达到高分时,我可以销毁标记为 DestroyUI 的对象。

我一直在谷歌搜索,但我不确定我做错了什么。

保存测试

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

public class SaveTest : MonoBehaviour
{
public ButtonReposition buttonrepositionscript;

void Start()
{
    DontDestroyOnLoad(gameObject);
    GameObject obj = GameObject.Find("ButtonReposition");
}

// Update is called once per frame
void Update()
{
    if (GetComponent<ButtonReposition.highscore <= 100)

    Destroy(GameObject.FindWithTag("DestroyUI"));
}
}

高分脚本

public class ButtonReposition : MonoBehaviour
{
public Button prefab;
public GameObject loseObject;
public int count;
public int highscore;
public Text countText;
public Text Highscoretext;
public Image lineimagestandard;

// Use this for initialization

void Start()
{
    PlayerPrefs.GetInt("scorePref");
    highscore = PlayerPrefs.GetInt("scorePref");
    SetHighscoretext();
    SetCountText();
    float x = Random.Range(325f, -600f);
    float y = Random.Range(250f, -450f);
    Debug.Log(x + "," + y);
    prefab.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
}

void Update()
{
    if (Highscoretext.name == "highscoretext")
    {
        Highscoretext.text = "highscore" + highscore;
    }

    PlayerPrefs.SetInt("scorePref", highscore);
}
4

1 回答 1

2

把它放在你的保存测试脚本上highscore = PlayerPrefs.GetInt("scorePref"); ,就是这样。

于 2017-03-10T08:39:52.410 回答