2014-01-22 82 views
0

我正在開發Unity上的2D遊戲。但是我收到了一些錯誤。我不知道如何解決這些問題。我是新手,並從一些鏈接參考。也許我正在創建檢查點,生成點,死亡區,這就是爲什麼會出現錯誤。如何解決這些錯誤

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) 
+1

也許一些JavaScript文件丟失,你應該添加到Assets文件夾。 –

+0

以及如何知道什麼JavaScript文件丟失? –

回答

0

在這一行錯誤:

變種SF = Camera.main.GetComponent(SM oothFollow);

檢查,如果SmoothFollow組件已被添加到主攝像機。可能問題在於,在這個聲明之後,var sf是null。

+0

當我運行該項目,然後根據PlayerRespawn腳本,我的球員已經死亡,然後創建新的球員,但玩家無法訪問smoothfollow腳本。 –

+0

併發生缺少參考例外 –

+0

我不理解您的意見。你能澄清他們嗎? –