2014-04-18 40 views
0

我不斷收到錯誤對象引用不設置到對象的實例上線64的情況下,我想不出什麼,我需要做的統一C#對象引用不設置到對象

一切工作就像生活都出現了和定時器和分數,但分數不增加,如果你能告訴我什麼是錯的,我會很感激

這裏是我的代碼:

using UnityEngine; 
using System.Collections; 

public class Player : MonoBehaviour { 

private CountdownTimer myTimer; 

private int score = 0; 
private int lives = 3; 
private int DEATH_Y = -10; 

public Texture2D LivesLeft1; 
public Texture2D LivesLeft2; 
public Texture2D LivesLeft3; 

public int GetScore(){ 
    return score; 
} 

public int GetLives() 
{ 
    return lives; 
} 

private void Start() 
{ 
    myTimer = GetComponent<CountdownTimer>(); 
} 

private void Update() 
{ 
    float y = transform.position.y; 

    if (y < DEATH_Y) { 
     MoveToStartPosition(); 
     lives--; 
    } 

    if (score == 10) 
    { 
     Application.LoadLevel("Level2"); 
    } 

    if (lives == 0) 
    { 
     Application.LoadLevel("GameOver"); 
    } 

} 

private void OnGUI() 
{ 
    GUILayout.BeginHorizontal(); 
    DisplayLives(); 

    int secondsLeft = myTimer.GetSecondsRemaining();//this is line 64 
    string timeMessage = "Seconds left = " + secondsLeft; 
    GUILayout.Label(timeMessage); 

    string scoreMessage = "Score = " + score; 
    GUILayout.Label (scoreMessage); 
} 

private void DisplayLives() 
{ 
    int playerLives = GetLives(); 

    if (1 == playerLives) { 
     GUILayout.Label(LivesLeft1); 
    } 

    if (2 == playerLives) 
    { 
     GUILayout.Label(LivesLeft2); 
    } 

    if(3 == playerLives){ 
     GUILayout.Label(LivesLeft3); 
    } 
} 

private void MoveToStartPosition() 
{ 
    Vector3 startPosition = new Vector3(0,5,0); 
    transform.position = startPosition; 
} 

/** 
* what increases the score 
* anything with the tag Hidden 
*/ 
private void OnTriggerEnter(Collider c) 
{ 
    string tag = c.tag; 

    if("Hidden" == tag) 
    { 
     score++; 
    } 
} 
} 
+0

在'Start()'之前調用OnGUI()'? –

+0

你能指出第64行嗎?根據編輯器,我只是將其複製到它的'private void DisplayLives()',它不能有這種異常... –

+0

這是64行根據MonoDeveloper int secondsLeft = myTimer.GetSecondsRemaining();' – user2961276

回答

1

您確定您的GameObject具有名爲CountdownTime的組件R' 此外,將開始功能更改爲喚醒,因爲該行不依賴於其他任何內容。

+0

謝謝我沒有注意到由於某種原因沒有添加,但現在有一些重疊的時間現在,我不能讀取它說什麼,但我認爲這是計時器和分數重疊有一些我需要的東西更改? – user2961276

+0

你是什麼意思重疊?您的GUI位置框? – Assassinbeast

+0

是的,就像它現在兩次在另一個上面寫下 – user2961276