繁体   English   中英

Unity C#对象引用未设置为对象的实例

[英]Unity C# Object reference not set to an instance of an object

我不断收到错误“对象引用”未设置为第64行上的对象实例的信息,我无法弄清楚需要做什么

一切都在运转,就像生活在不断地出现,计时器和分数在增长,但如果您能告诉我什么地方出了错,分数却没有增加,我将非常感激

这是我的代码:

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++;
    }
}
}

您确定您的GameObject具有名为CountdownTimer的组件吗? 另外,将“开始”功能更改为“唤醒”,因为该行不依赖于其他任何内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM