Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.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# 当另一个脚本处于活动状态时禁用脚本_C#_Unity3d - Fatal编程技术网

C# 当另一个脚本处于活动状态时禁用脚本

C# 当另一个脚本处于活动状态时禁用脚本,c#,unity3d,C#,Unity3d,所以我有两个场景切换脚本,它们都连接到两个不同的游戏对象上。但由于某些原因,一个脚本会影响两个游戏对象。因此,当另一个脚本在另一个游戏对象上处于活动状态时,我尝试暂时禁用其中一个脚本。这是我所拥有的,但它返回了一个错误,“BasketballSceneChange”是一个类型,在给定的上下文中无效 }更改此选项: GameObject.Find(“pPrism1”).GetComponent(BasketballSceneChange).enabled=false 到 将myBaseClass更

所以我有两个场景切换脚本,它们都连接到两个不同的游戏对象上。但由于某些原因,一个脚本会影响两个游戏对象。因此,当另一个脚本在另一个游戏对象上处于活动状态时,我尝试暂时禁用其中一个脚本。这是我所拥有的,但它返回了一个错误,“BasketballSceneChange”是一个类型,在给定的上下文中无效

}更改此选项:

GameObject.Find(“pPrism1”).GetComponent(BasketballSceneChange).enabled=false

将myBaseClass更改为显示场景的类。

更改此项:

GameObject.Find(“pPrism1”).GetComponent(BasketballSceneChange).enabled=false


将myBaseClass更改为显示场景的类。

可以避免使用GetComponent仅声明公共变量并从检查器分配它

public AnotherScript theOtherScript;
void Update()
{
   if(theOtherScript != null && theOtherScript.enabled)
   {
      theOtherScript.enabled = false;
   }
}
检查这个

另一种方法是找到游戏对象并获取组件

我再次阅读了您的代码,发现错误并没有编译该行:

GameObject.Find("pPrism1").GetComponent(BasketballSceneChange).enabled = false;
应该是

GameObject.Find("pPrism1").GetComponent<BasketballSceneChange>().enabled = false;
GameObject.Find(“pPrism1”).GetComponent().enabled=false;

您可以避免使用GetComponent仅仅声明一个公共变量并从检查器分配它

public AnotherScript theOtherScript;
void Update()
{
   if(theOtherScript != null && theOtherScript.enabled)
   {
      theOtherScript.enabled = false;
   }
}
检查这个

另一种方法是找到游戏对象并获取组件

我再次阅读了您的代码,发现错误并没有编译该行:

GameObject.Find("pPrism1").GetComponent(BasketballSceneChange).enabled = false;
应该是

GameObject.Find("pPrism1").GetComponent<BasketballSceneChange>().enabled = false;
GameObject.Find(“pPrism1”).GetComponent().enabled=false;

您会遇到什么错误?要么是
GetComponent(typeof(BasketballSceneChange))
要么是
GetComponent()
-文档中的示例中清楚地强调了这一点:您会遇到什么错误?要么是
GetComponent(typeof(BasketballSceneChange))
要么是
GetComponent()
-文档中的示例中明确强调了这一点:否,
GetComponent(BasketballSceneChange)
不会compile@UnholySheep请在我的回复中检查我的编辑,您必须编写.GetComponent().enabledNo,
GetComponent(BasketballSceneChange)
将不会compile@UnholySheep在我的响应中检查我的编辑,您必须写入.GetComponent().enabled