关于官方教程Space Shooter子弹生成部分提示MissingReferenceException的解决办法

Space Shooter 第七节中子弹生成部分提示Error

MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.

尝试调用的项目GameObject已被销毁

具体表现为当屏幕中所有子弹都因越过边界被销毁时,应用于子弹生成的GameObject却显示为bolt(clone)(clone)(clone)(clone),项目不存在导致Error。

但按照官方教程的严谨性应该不会出现这样低级的错误。

解决方法如下(仅修改第18,28,29行处即可):


using UnityEngine;
using System.Collections;

[System.Serializable]
public class Boundary
{
	public float xMin, xMax, zMin, zMax;
}


public class PlayerController : MonoBehaviour 
{

	public float speed;
	public float tilt;
	public Boundary boundary;

	public GameObject bolt;
	public GameObject shotSpawn; 
	public float fireRate;

	private float nextFire; 

	void Update () 
	{
		if (Input.GetKey("space") && Time.time > nextFire && shotSpawn != null) 
		{
			GameObject shot = Instantiate(bolt) as GameObject;
			shot.name = bolt.name;
			nextFire = Time.time + fireRate;
			shot.transform.position = shotSpawn.transform.position;

		}
	}
	void FixedUpdate ()
	{

		float moveHorizontal = Input.GetAxis ("Horizontal");
		float moveVertical = Input.GetAxis ("Vertical");

		Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
		GetComponent<Rigidbody>().velocity = movement * speed;

		GetComponent<Rigidbody>().position = new Vector3
			(
				Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax), 0.0f, Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
			);
		GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值