Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Unity中启用/禁用预制组件_C#_Unity3d - Fatal编程技术网

C# 在Unity中启用/禁用预制组件

C# 在Unity中启用/禁用预制组件,c#,unity3d,C#,Unity3d,正如您在脚本中看到的,在销毁功能中,我禁用了Prefact的组件,它在游戏过程中运行良好,但在停止游戏后它仍然处于禁用状态,我知道原因是它是Prefact,因此它保存了该信息,但如何仅在游戏正在玩时禁用它?我已经考虑过while循环,但它崩溃了,还有唤醒功能。有什么想法吗 private void OnDestroy() { Lose = true; ENEMIES.GetComponent<EnemiesMovement>().unit = 0.0f; pr

正如您在脚本中看到的,在销毁功能中,我禁用了Prefact的组件,它在游戏过程中运行良好,但在停止游戏后它仍然处于禁用状态,我知道原因是它是Prefact,因此它保存了该信息,但如何仅在游戏正在玩时禁用它?我已经考虑过while循环,但它崩溃了,还有唤醒功能。有什么想法吗

private void OnDestroy()
{
    Lose = true;
    ENEMIES.GetComponent<EnemiesMovement>().unit = 0.0f;
    prefenBullet.GetComponent<MeshRenderer>().enabled = false;
    prefenBullet.GetComponent<BoxCollider2D>().enabled = false;
}
private void OnDestroy()
{
失去=真实;
.GetComponent().unit=0.0f;
prefenBullet.GetComponent().enabled=false;
prefenBullet.GetComponent().enabled=false;
}

将实例保存在GameObject变量中,以便以后在代码中访问和修改。大概是这样的:

// Reference to the Prefab. Drag a Prefab into this field in the Inspector.
public GameObject myPrefab;
GameObject myPrefabInstance;

    // This script will simply instantiate the Prefab when the game starts.
void Start()
{
    // Instantiate at position (0, 0, 0) and zero rotation.
    myPrefabInstance = Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
}

private void OnDestroy()
{
    myPrefabInstance.GetComponent<MeshRenderer>().enabled = false;
    myPrefabInstance.GetComponent<BoxCollider2D>().enabled = false;
}
//对预置的引用。在Inspector中将一个预置拖动到此字段中。
公共游戏对象myprefare;
游戏对象实例;
//这个脚本将在游戏开始时简单地实例化预置。
void Start()
{
//在位置(0,0,0)和零旋转处实例化。
MyPrefableInstance=实例化(MyPrefable,新向量3(0,0,0),四元数.identity)作为游戏对象;
}
私有void OnDestroy()
{
MyPrefableInstance.GetComponent().enabled=false;
MyPrefableInstance.GetComponent().enabled=false;
}

为什么不在运行时实例化预置?然后在我实例化的时候修改或删除那个实例,我怎样才能取消实例化过程呢?