我正在开发Unity上的二维游戏。但是我收到了一些错误。我不知道如何解决这些问题。我是新手,并从一些链接获取参考。也许我正在创建检查站,产生点,死亡区,这就是为什么会出错。
PlayerRespawn.js -
var Player : GameObject;
var spawnPoint: Transform;
function OnTriggerEnter(other : Collider) {
Destroy(other.gameObject);
var P: GameObject = Instantiate(Player,spawnPoint.position,Quaternion.identity);
var sf=Camera.main.GetComponent(SmoothFollow);
sf.target=P.transform;
}
SmoothFollow.js -
var target : Transform;
// The distance in the x-z plane to the target
var distance = 10.0;
// the height we want the camera to be above the target
var height = 5.0;
// How much we
var heightDamping = 2.0;
var rotationDamping = 3.0;
// Place the script in the Camera-Control group in the component menu
@script AddComponentMenu("Camera-Control/Smooth Follow")
function LateUpdate () {
// Early out if we don't have a target
if (!target)
return;
// Calculate the current rotation angles
var wantedRotationAngle = target.eulerAngles.y;
var wantedHeight = target.position.y + height;
var currentRotationAngle = transform.eulerAngles.y;
var currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position.y = currentHeight;
// Always look at the target
transform.LookAt (target);
}
我得到的错误是 -
NullReferenceException
PlayerRespawn.OnTriggerEnter (UnityEngine.Collider other) (at Assets/scripts/PlayerRespawn.js:8)
答案 0 :(得分:0)
在这一行:
var sf = Camera.main.GetComponent(SmoothFollow);
检查SmoothFollow组件是否已添加到主摄像头。问题可能是在此语句之后,var sf为null。