Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

I need some help with an error pls?

Discussion in '2D' started by Steev98, Jun 16, 2017.

  1. Steev98

    Steev98

    Joined:
    Jun 16, 2017
    Posts:
    16
    Hey,

    I´m new to Unity and I´m making a 2D Jump & Run and I try to add lives in the LevelManager, but I always get an error:
    NullReferenceException: Object reference not set to an instance of an object
    LevelManager.Start () (at Assets/Scripts/LevelManager.cs:20)

    That´s my script:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7.  
    8. public class LevelManager : MonoBehaviour
    9. {
    10.  
    11.     public GameObject currentCheckpoint;
    12.     //Player GameObject
    13.     //aktueller Checkpoint
    14.     public GameObject player;
    15.     //Lebensanzeige
    16.     public int lives;
    17.     public Text livesText;
    18.  
    19.     private void Start()
    20.     {
    21.         LivesText.text = "Lives: " + lives.ToString();
    22.     }
    23.  
    24.     private void Update() {
    25.  
    26.         if (Input.GetKeyDown(KeyCode.Escape)) {
    27.             Debug.Log("Game ends!");
    28.             Application.Quit();
    29.         }
    30.   }
    31.  
    32.  
    33.     public void RespawnPlayer()
    34.     {
    35.         // Leben abziehen
    36.         lives = lives - 1;
    37.         //Lebensanzeige aktualisieren
    38.         LivesText.text = "Lives: " + lives.ToString();
    39.         //Überprüfen ob Spieler noch Leben hat
    40.         if(lives > 0)
    41.         {
    42.             player.transform.position = currentCheckpoint.transform.position;
    43.         }
    44.         else
    45.         {
    46.             Time.timeScale = 0.0f;
    47.             Debug.Log("Game over!");
    48.         }
    49.  
    50.         //falls ja -> Respawn
    51.         //falls nein -> Spielende
    52.  
    53.  
    54.         // Spieler an die Postition des Checkpoints bringen.
    55.    
    56.     }
    57. }
    58.  
    when i remove this: livesText.text = lives.ToString(); and LivesText.text = "lives: " + lives.ToString(); the game works.

    I pulled it into the LevelManager in the Inspector but it sollt gives me this error.

    Please help me :).

    Greeting from Austria
     
    Last edited: Jun 16, 2017
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
  3. Steev98

    Steev98

    Joined:
    Jun 16, 2017
    Posts:
    16
    Do you mean pulling the LivesText into the LevelManager? Then yes but it still don´t works.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    This post was duplicated (which, btw, you probably shouldn't do). It's been solved :)