【问题标题】:Instantiated prefab shows up in hierarchy as inactive实例化的预制件在层次结构中显示为非活动
【发布时间】:2021-08-06 19:02:17
【问题描述】:

我的预制枪在实例化后在我的层次结构中显示为非活动状态。

void IInteractableTarget.Interact(GameObject character)
{
    int winningInt = Random.Range(1, 3);

    if(winningInt == 1)
    {
        GameObject obj_rifle = Instantiate(assaultRiflePrefab, spawnPos.position, spawnPos.rotation);
    }

    if (winningInt == 2)
    {
        GameObject obj_pistol = Instantiate(pistolPrefab, spawnPos.position, spawnPos.rotation);
    }

    //GameObject obj = Instantiate(assaultRiflePrefab, spawnPos.position, spawnPos.rotation);
    //obj.GetComponent<Rigidbody>().AddForce(transform.up * 5, ForceMode.Impulse);
    //Instantiate(assaultRiflePrefab, spawnPos.position, spawnPos.rotation);

    Debug.Log("Weapon from chest instantiated" + " " + winningInt);
}

我应该如何处理?

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    来自文档https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

    克隆时游戏对象的活动状态是 维护,因此如果原始文件处于非活动状态,则克隆将在 非活动状态。

    您可以将游戏对象的活动状态设置为true

    void IInteractableTarget.Interact(GameObject character)
    {
        int winningInt = Random.Range(1, 3);
    
        if(winningInt == 1)
        {
            GameObject obj_rifle = Instantiate(assaultRiflePrefab, spawnPos.position, spawnPos.rotation);
            obj_rifle.SetActive(true); // <-- Set active to true
        }
    
        if (winningInt == 2)
        {
            GameObject obj_pistol = Instantiate(pistolPrefab, spawnPos.position, spawnPos.rotation);
            obj_pistol.SetActive(true); // <-- Set active to true
        }
    
        //GameObject obj = Instantiate(assaultRiflePrefab, spawnPos.position, spawnPos.rotation);
        //obj.GetComponent<Rigidbody>().AddForce(transform.up * 5, ForceMode.Impulse);
        //Instantiate(assaultRiflePrefab, spawnPos.position, spawnPos.rotation);
    
        Debug.Log("Weapon from chest instantiated" + " " + winningInt);
    }
    

    【讨论】:

    • 我之前试过了,但没用。但我又试了一次,它仍然在场景中显示为非活动状态。
    • 我能想到的唯一原因是您在其他地方将其活动状态设置为false。也许预制件有一个脚本将自己设置为非活动状态?
    • 第 3 方资产必须有一些东西。现在它每尝试 1/5 次就可以工作......? :s
    • 这是第 3 方资产立即停用预制件,因为我的角色碰撞器在预制件触发碰撞器内。
    猜你喜欢
    • 2017-07-15
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多