如何在unity中只实例化一个对象

How to instantiate only one object in unity

int winer;

void Start() {
    winer = PlayerPrefs.SetInt("win", 0);
}

void Update() {
    winer = PlayerPrefs.GetInt("win");
    if(spawnPoint1.transform.position.x <= 10) {
        PlayerPrefs.SetInt("win", 1);
        chunks.enabled = false;
        if(winer == 1) {
            Instantiate(win, winPoint.transform.position, transform.rotation);
        }
    } else {
        PlayerPrefs.SetInt("win", 0);
    }
}

如何为二维精灵只实例化一个对象?

感谢您的回复

您可以跟踪变量中的状态,以便实例化只发生一次。

bool instantiated; //is false by default
int winer;

void Start() {
    winer = PlayerPrefs.SetInt("win", 0);
}

void Update() {
    winer = PlayerPrefs.GetInt("win");
    if(spawnPoint1.transform.position.x <= 10) {
        PlayerPrefs.SetInt("win", 1);
        chunks.enabled = false;
        if(winer == 1 && !intstantiated) {
            Instantiate(win, winPoint.transform.position, transform.rotation);
            intstantiated = true;
        }
    } else {
        PlayerPrefs.SetInt("win", 0);
    }
}