【问题标题】:How to instantiate only one object in unity [closed]如何在统一中仅实例化一个对象[关闭]
【发布时间】:2021-07-25 10:23:26
【问题描述】:
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);
    }
}

如何只为 2d 精灵实例化一个对象?

感谢您的回复

【问题讨论】:

  • How to instantiate only one object in unity -> 只调用Instantiate 一次? ^^

标签: unity3d 2d instantiation


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 2018-12-13
    • 1970-01-01
    相关资源
    最近更新 更多