我需要取一个脚本中设置的 bool 值(放入一个名为“bouclier”的变量中)来启用或禁用游戏对象。
变量位于游戏对象 Player 中(此处右下角):
我需要启用或禁用此游戏对象(“Bouclier01”):
为此,我将脚本附加到游戏对象“Bouclier01”。这里是:
using UnityEngine;
using System.Collections;
public class ShowBouclier : MonoBehaviour {
public GameObject Bouclier01;
public bool bouclier;
// Use this for initialization
void Start () {
Bouclier01 = Bouclier01.GetComponent<GameObject>();
}
// Update is called once per frame
void Update () {
Bouclier01.enabled = false;
if (bouclier == true) {
Bouclier01.enabled = true;
}
}
}
我一定遗漏了一些东西,因为这会出现以下错误消息:
知道如何正确完成此操作吗?
最佳答案
您可以使用 GameObject.SetActive() 函数来激活或停用游戏对象(我认为 GameObject.enabled 在旧 API 中):
Bouclier.SetActive(false);
顺便说一下,如果你想知道一个游戏对象的当前激活状态,可以使用 GameObject.activeSelf,它是一个只读变量:
Debug.Log(Bouclier.activeSelf);
关于c# - 从脚本启用/禁用游戏对象组件 [Unity3D],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36238795/