C# 如何使用GetComponent语法

C# 如何使用GetComponent语法,c#,unity3d,gameobject,C#,Unity3d,Gameobject,我试图理解GetComponent,但我很难理解如何编写语法。我有两个脚本(SpawnBehavior和SpotClicked),希望将bool从“SpawnBehavior”转换为SpotClicked 如何正确使用语法,并将SpawnBehavior中的布尔值更改为true void OnMouseDown() { screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);

我试图理解GetComponent,但我很难理解如何编写语法。我有两个脚本(SpawnBehavior和SpotClicked),希望将bool从“SpawnBehavior”转换为SpotClicked

如何正确使用语法,并将SpawnBehavior中的布尔值更改为true

void OnMouseDown()
{
      screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
      offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));

    if(this.gameObject.tag == "fishSpot"){
          Debug.Log("Clicked "+gameObject.name);
          //get "stopped"bool from "SpawnBehaviour" script and set it to true
          SpawnBehaviour sb = spawnPoint.GetComponent<SpawnBehaviour>().stoppedSpawn=true;
    }
}
不幸的是,它没有向您展示C#中的示例,但它非常直截了当

你最终会做的是

SpawnBehavior sb = gameObject.GetComponent<SpawnBehaviour>();
SpotClicked sc = gameObject.GetComponent<SpotClicked>();
//Do whatever you want with the variables from either MonoBehaviour
但是,如果你可以保存一些击键和强制转换,为什么不呢

当然,如果您要多次访问这些组件,您可以将它们缓存在
Start()
中。调用GetComponent代价很高,例如,如果您最后每帧都要调用一次,则更是如此

如果您随后想要为spawneBehavior将布尔变量设置为true,您可以这样做

SpawnBehaviour sb = gameObject.GetComponent<SpawnBehaviour>();
sb.stoppedSpawn = true;
sb=gameObject.GetComponent();
sb.stoppedpawn=true;
或者,如果你不想保持产卵行为,你可以这样做

gameObject.GetComponent<SpawnBehaviour>().stoppedSpawn = true;
gameObject.GetComponent().stoppedpawn=true;
但是,如果您在其他地方需要它,或者经常需要它,请将其缓存。

不幸的是,它没有向您展示C#中的示例,但它非常简单

你最终会做的是

SpawnBehavior sb = gameObject.GetComponent<SpawnBehaviour>();
SpotClicked sc = gameObject.GetComponent<SpotClicked>();
//Do whatever you want with the variables from either MonoBehaviour
但是,如果你可以保存一些击键和强制转换,为什么不呢

当然,如果您要多次访问这些组件,您可以将它们缓存在
Start()
中。调用GetComponent代价很高,例如,如果您最后每帧都要调用一次,则更是如此

如果您随后想要为spawneBehavior将布尔变量设置为true,您可以这样做

SpawnBehaviour sb = gameObject.GetComponent<SpawnBehaviour>();
sb.stoppedSpawn = true;
sb=gameObject.GetComponent();
sb.stoppedpawn=true;
或者,如果你不想保持产卵行为,你可以这样做

gameObject.GetComponent<SpawnBehaviour>().stoppedSpawn = true;
gameObject.GetComponent().stoppedpawn=true;

但是,如果您在其他任何地方需要它,或者经常需要它,请缓存它。

从概念上讲,您应该首先了解什么是组件,什么是游戏对象,然后不难获得正确的语法:

例如:

var layer = someGameObject.GetComponent<GUILayer>();
var layer=someGameObject.GetComponent();

从概念上讲,您应该首先了解什么是组件,什么是游戏对象,然后不难理解正确的语法:

例如:

var layer = someGameObject.GetComponent<GUILayer>();
var layer=someGameObject.GetComponent();

I keep get“无法隐式键入'bool'到'spawneBehavior',请显示您正在使用的代码。编辑成你的问题。更新我的答案@Eyrik。你不能做你想做的双重任务交易。最后,您会将一个布尔值指定给SpawnBehavior,这正是它所抱怨的。按照上面的两个步骤来做,你就可以开始了。非常感谢。现在我可以继续了:完成问题更多:我得到了一个循环脚本,只要stoppedSpawn为false,它就会生成对象。当我单击对象时,脚本将布尔值变为false,但之后它又直接变为true,而没有任何脚本说它将stoppedSpawn变回true“无法隐式地将'bool'键入'spawnebehavior',请显示您正在使用的代码。编辑成你的问题。更新我的答案@Eyrik。你不能做你想做的双重任务交易。最后,您会将一个布尔值指定给SpawnBehavior,这正是它所抱怨的。按照上面的两个步骤来做,你就可以开始了。非常感谢。现在我可以继续了:完成问题更多:我得到了一个循环脚本,只要stoppedSpawn为false,它就会生成对象。当我单击对象时,脚本将布尔值变为false,但之后它又直接变为true,而没有任何脚本说它将stoppedSpawn变回true。