C# 为什么可以';我将一个游戏对象分配给另一个游戏对象的公共变量
这是玩家脚本中的方法,它捕获光标下的敌人并创建一个导弹来跟踪它C# 为什么可以';我将一个游戏对象分配给另一个游戏对象的公共变量,c#,unity3d,C#,Unity3d,这是玩家脚本中的方法,它捕获光标下的敌人并创建一个导弹来跟踪它 if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, 10, 1<<8)) { FollowEnemy missile = Instan
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 10, 1<<8))
{
FollowEnemy missile = Instantiate(bullet, transform.position, transform.rotation) as FollowEnemy;
// runs perfectly, can change the value of hit.transform
Transform temp = hit.transform;
temp.position = transform.position;
// enemy is a public variable(GameObject)
missile.enemy = temp.gameObject;
// and here comes "NullReferenceException: Object reference not set to an instance of an object"
}
}
if(Input.GetMouseButtonDown(0))
{
Ray-Ray=Camera.main.screenpointoray(输入.鼠标位置);
雷卡斯特击中;
如果(Physics.Raycast)(光线,命中率,10,1你不想将你的导弹实例化为FollowEnmy
,而是作为游戏对象,其中包含组件FollowMissile
GameObject missile = (GameObject)Instantiate(bullet, transform.position, transform.rotation);
...
missile.GetComponent<FollowMissile>().enemy = temp.gameObject;
GameObject导弹=(GameObject)实例化(子弹,transform.position,transform.rotation);
...
employee.GetComponent().敌方=临时游戏对象;
大约5个小时后,我终于发现了一个愚蠢的错误:
public Rigidbody bullet;
哪个应该是GameObject
bullet
是预制的?需要是分配给公共变量的游戏对象,并附带followerenemy
脚本。它已修复。无论如何谢谢你^-^+1给Gunnar B。@GunnarB。你没有找到答案。Gunnar B找到了答案,他的答案是ex事实上,你把什么作为你的解决方案。他说把东西改成游戏对象,这就解决了你的问题。接受这个答案就好了。。。。