从另一个场景中的播放器首选项访问变量

时间:2017-03-07 01:35:57

标签: c# unity2d

我使用playerprefs

在我的场景“标准游戏”中管理我的HighScore

我需要在我的另一个脚本“SaveTest”中访问我的变量HighScore,这个脚本位于一个名为“Main Menu”的不同场景中。因此,当达到高分时,我可以销毁标记为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"));
}
}

HighScore脚本

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

1 个答案:

答案 0 :(得分:2)

将其放在保存测试脚本highscore = PlayerPrefs.GetInt("scorePref");上 就是这样。