0

I keep getting the error Object reference not set to an instance of an object on line 64 and I can't figure out what I need to do

Everything is working like the lives are showing up and the timer and the score is but the score isn't increasing if you can tell me what is wrong I would be so thankful

Here's my code:

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++;
    }
}
}
4
  • Probably OnGUI() is called before Start()? Commented Apr 18, 2014 at 15:29
  • Could you point out where line 64 is? According to the editor I just copied this into, its private void DisplayLives() which can't have that exception... Commented Apr 18, 2014 at 15:31
  • This is line 64 according to MonoDeveloper int secondsLeft = myTimer.GetSecondsRemaining(); Commented Apr 18, 2014 at 15:32
  • That says myTimer is null. Your OnGUI() is definitely being called before Start() or GetComponent<CountdownTimer>() is returning null. Perhaps some breakpoints in those general areas and single stepping the application would help you to debug this? Commented Apr 18, 2014 at 15:34

1 Answer 1

1

Are you sure that your GameObject have the Component called CountdownTimer? Also, change the Start function to Awake, because that line is not depending on anything else.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks I didn't notice that wasn't added for some reason but now there is something overlapping the time now and I can't read what it says but I think it's the timer and score overlapping again is there something that I need to change?
What do you mean by overlapping? Your GUI position boxes?
Yeah it's like it's writing it twice on top of each other now
Ok, but then figure out why they position the way they do. Experiment with it.
Looking closer at it it looks like it's counting down and counting up so the timer is increasing and also decreasing at the same time on top of each other

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.