obj.GetComponent<GameObject>().SetActive == false
时间: 2023-08-21 11:05:02 浏览: 40
这段代码存在一些问题,应该修改为以下形式:
```
obj.SetActive(false);
```
SetActive是GameObject自带的一个方法,用于启用或禁用一个GameObject对象。它的参数是一个布尔值,true表示启用对象,false表示禁用对象。因此,可以直接通过obj.SetActive(false)来禁用一个GameObject对象,而不需要使用GetComponent方法来获取一个GameObject组件。
如果要启用一个GameObject对象,可以使用obj.SetActive(true)。需要注意的是,SetActive方法只能用于激活或禁用整个GameObject对象,而不能用于激活或禁用某个组件或子对象。如果需要激活或禁用某个组件或子对象,可以通过获取该组件或子对象的引用,并设置其enabled属性来实现。
相关问题
obj.GetComponent<GameObject>().SetActive == false怎么改成bool形式
将代码修改为以下形式:
```
bool isActive = obj.activeSelf;
if (isActive) {
obj.SetActive(false);
}
```
在这个示例中,我们使用了GameObject的activeSelf属性来获取当前GameObject对象的激活状态,并将其保存在一个名为isActive的变量中。然后,我们检查isActive变量的值是否为true,如果是,则通过SetActive方法将对象禁用。需要注意的是,activeSelf属性是只读的,因此不能对其进行赋值操作。
另外,代码中的GetComponent方法也存在问题,应该将其修改为以下形式:
```
obj.GetComponent<Component>();
```
GetComponent方法用于获取GameObject对象上的指定组件,因此应该传递一个组件类型作为参数,而不是GameObject类型。在这个示例中,如果我们需要获取GameObject对象的Transform组件,可以使用以下代码:
```
Transform transform = obj.GetComponent<Transform>();
```
public GameObject img1; public GameObject img2; public GameObject img3; public GameObject MOve; public GameObject Rotate; public GameObject Scale; public void Move() { if(MOve.GetComponent<MOVEWIN>().enabled == false) { img1.SetActive(true); MOve.GetComponent<MOVEWIN>().enabled = true; img2.SetActive(false); Rotate.GetComponent<Rotet>().enabled = false; img3.SetActive(false); GameObject[] enemies = GameObject.FindGameObjectsWithTag("MOVEWIN"); foreach (GameObject Scale in enemies) { Destroy(Scale.GetComponent<Scale>()); } } else { img1.SetActive(false); MOve.GetComponent<MOVEWIN>().enabled = false; } } public void Rota() { if (Rotate.GetComponent<Rotet>().enabled == false) { img2.SetActive(true); Rotate.GetComponent<Rotet>().enabled = true; img1.SetActive(false); MOve.GetComponent<MOVEWIN>().enabled = false; img3.SetActive(false); GameObject[] enemies = GameObject.FindGameObjectsWithTag("MOVEWIN"); foreach (GameObject Scale in enemies) { Destroy(Scale.GetComponent<Scale>()); } } else { img2.SetActive(false); Rotate.GetComponent<Rotet>().enabled = false; } unity 整理代码 功能不变 缩短
以下是优化后的代码:
public GameObject img1;
public GameObject img2;
public GameObject img3;
public GameObject MOve;
public GameObject Rotate;
public GameObject Scale;
public void Move()
{
bool moveEnabled = MOve.GetComponent<MOVEWIN>().enabled;
img1.SetActive(!moveEnabled);
MOve.GetComponent<MOVEWIN>().enabled = !moveEnabled;
img2.SetActive(false);
Rotate.GetComponent<Rotet>().enabled = false;
img3.SetActive(false);
DestroyAllObjectsWithTag("MOVEWIN");
}
public void Rota()
{
bool rotateEnabled = Rotate.GetComponent<Rotet>().enabled;
img2.SetActive(!rotateEnabled);
Rotate.GetComponent<Rotet>().enabled = !rotateEnabled;
img1.SetActive(false);
MOve.GetComponent<MOVEWIN>().enabled = false;
img3.SetActive(false);
DestroyAllObjectsWithTag("MOVEWIN");
}
private void DestroyAllObjectsWithTag(string tag)
{
GameObject[] objects = GameObject.FindGameObjectsWithTag(tag);
foreach (GameObject obj in objects)
{
Destroy(obj.GetComponent<Scale>());
}
}
这样,代码更加简洁易读,而且功能不变。