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

How can I fix this error?

Discussion in 'Scripting' 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. public class LevelManager : MonoBehaviour
    7. {
    8.     public GameObject currentCheckpoint;
    9.     //Player GameObject
    10.     //aktueller Checkpoint
    11.     public GameObject player;
    12.     //Lebensanzeige
    13.     public int lives;
    14.     public Text livesText;
    15.     private void Start()
    16.     {
    17.         LivesText.text = "Lives: " + lives.ToString();
    18.     }
    19.     private void Update() {
    20.         if (Input.GetKeyDown(KeyCode.Escape)) {
    21.             Debug.Log("Game ends!");
    22.             Application.Quit();
    23.         }
    24.   }
    when i remove this: livesText.text = lives.ToString(); and LivesText.text = "lives: " + lives.ToString(); the game works but the lives don´t do anything.

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

    Please help me :).

    Greeting from Austria
     
  2. sylon

    sylon

    Joined:
    Mar 5, 2017
    Posts:
    246
    I think you just have a typo.
    your definition is : public Text livesText;
    in code : LivesText.text = ...
    -> change that to livesText.text = ...

    Hope it helps.
     
    cstooch likes this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    You do have a typo, and that may fix it. But if you are getting a object not set to a instance error, that would be a different type of error.

    Make sure you have dragged something into your livesText in the inspector as well (if @sylon suggestion doesn't fix it)
     
  4. Steev98

    Steev98

    Joined:
    Jun 16, 2017
    Posts:
    16
    Hey, thanks for the help but it didn´t work I already checked it and changed some things. Here is the new Script, it still don´t works :(. I checked the inspector and in Live Text is LiveText(Text) but it just won´t work.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. public class LevelManager : MonoBehaviour
    8. {
    9.  
    10.     public GameObject currentCheckpoint;
    11.     //Player GameObject
    12.     //aktueller Checkpoint
    13.     public GameObject player;
    14.     //Lebensanzeige
    15.     public int live;
    16.     public Text LiveText;
    17.  
    18.     public void Start()
    19.     {
    20.  
    21.         LiveText.text = "lives: " + live.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.         live = live - 1;
    37.         //Lebensanzeige aktualisieren
    38.         LiveText.text = "lives: " + live.ToString();
    39.        
    40.         //Überprüfen ob Spieler noch Leben hat
    41.         if (live > 0)
    42.         {
    43.             //falls ja -> Respawn
    44.             player.transform.position = currentCheckpoint.transform.position;
    45.         }
    46.         else
    47.         {
    48.             Time.timeScale = 0.0f;
    49.             Debug.Log("Game over!");
    50.         }
    51.  
    52.        
    53.         //falls nein -> Spielende
    54.  
    55.  
    56.         // Spieler an die Postition des Checkpoints bringen.
    57.        
    58.     }
    59. }
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Just saying it doesn't work, doesn't help. Are you getting a new error? What is happening? Or not happening?
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    This still not working?
     
  7. Steev98

    Steev98

    Joined:
    Jun 16, 2017
    Posts:
    16
    It´s still the same Error: NullReferenceException: Object reference not set to an instance of an object
    LevelManager.Start () (at Assets/Scripts/LevelManager.cs:20)
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    And you're sure it's set in the inspector?
    If you add, just before the error line:
    Code (csharp):
    1.  if (LiveText == null) print("Live Text is null here.");
    Does that print out in the console?

    I'm having a vague recollection of a potential issue if all of the above is true. That is, did you change the type of variable that LiveText was, at any point? If so (or even if not, but for good measure). Select it in the inspector, clear it (set it to nothing for the variable), then drag it back in from the hiearchy. :)
     
  9. Steev98

    Steev98

    Joined:
    Jun 16, 2017
    Posts:
    16
    Nope :/
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    So, did you look at my latest post? Hard to tell b/c you quoted an earlier post..
     
  11. Steev98

    Steev98

    Joined:
    Jun 16, 2017
    Posts:
    16

    I did that but it stil gives me the Error, so I don´t know what´s wrong. :(
    It says Live Text is null here in Console, but the Error is still there.

    Code (CSharp):
    1.  public GameObject currentCheckpoint;
    2.     //Player GameObject
    3.     //aktueller Checkpoint
    4.     public GameObject player;
    5.     //Lebensanzeige
    6.     public int live;
    7.     public Text liveText;
    8.  
    9.       void Start()
    10.  
    11.     {
    12.         if (liveText == null) print("Live Text is null here.");
    13.         liveText.text = live.ToString();
    14.              
    15.     }
    16.  
    17.     private void Update() {
    18.  
    19.         if (Input.GetKeyDown(KeyCode.Escape)) {
    20.             Debug.Log("Game ends!");
    21.             Application.Quit();
    22.         }
    23.   }  
    24.  
    25.  
    26.     public void RespawnPlayer()
    27.     {
    28.  
    29.         // Leben abziehen
    30.         live = live - 1;
    31.         // Lebensanzeige aktualisieren
    32.        liveText.text = live.ToString();
    33.         // Überprüfen ob Spieler noch Leben hat
    34.         if (live > 0)
    35.         {
    36.             // falls ja -> Respawn
    37.             // spieler an die position des checkpoints bringen
    38.             player.transform.position = currentCheckpoint.transform.position;
    39.         }
    40.         else
    41.         {
    42.             // falls nein -> Spielende!
    43.             Time.timeScale = 0.0f;
    44.             Debug.Log("Game over");
    45.         }
    46.  
    47.     }
    48. }
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    but the error is still there.. hm.. yes, well if it's null that's the problem ;)
    Did you double check the inspector & try remove the reference in the inspector & then re-assigning it from the hierarchy?
     
  13. Steev98

    Steev98

    Joined:
    Jun 16, 2017
    Posts:
    16
    What do you mean null is the problem?
    Yes I did that like 10x and it just won´t work, F***ing frustrating :/.


    http://imgur.com/a/lBjrr
    Thats the inspector.
     
  14. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Are you certain you don't have this component twice in your scene? One of which doesn't have livesText assigned.

    The problem is you have a null value for livesText, which means it is not assigned.
     
  15. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    As @DanielQuick suggested, you may have another copy of the script in your scene. Use the search feature of the hierarchy to search for your LevelManager script to ensure you only see one copy in the scene.
     
    Last edited: Jun 18, 2017
  16. Steev98

    Steev98

    Joined:
    Jun 16, 2017
    Posts:
    16
    I can´t find a second one and its assigned if that means that its like in the screenshot.

    http://imgur.com/a/OKM9H
     
  17. Steev98

    Steev98

    Joined:
    Jun 16, 2017
    Posts:
    16
    I can´t find a second one :(.

    http://imgur.com/a/OKM9H
     
  18. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sorry, not sure what to say. If you feel like attaching your project (which looks super small), I'd look at it whenever I'm free. :)
    You tried everything, including the second script possibility which was good to check.. after that, no idea what could be wrong, sorry.
     
    Steev98 likes this.
  19. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Agreed, I'd have to look at the project myself as well. The only other thing I can think of is another script is doing something somewhere that is either destroying the LevelManager script, thus breaking your references. Or, something else is setting the text variable to null.

    The third possibility is a simple unity glitch, but generally I don't experience those on something as simple as this.
     
    Steev98 likes this.
  20. Steev98

    Steev98

    Joined:
    Jun 16, 2017
    Posts:
    16

    Well thanks for the help :). I made something work now, a little bit ^^ if I go to my Charakter and give him Level Manager assigne the liveText and then delete the levelManager script out of my charakter then I can start the game without error, but I still don´t lose lives when i fall down or hit an enemy.

    I would just make a new script if I would knew how but I´m new so its hard :D. I have this small heart icon on my screen and i want to lose 1 live when i die, but I can´t find a tutorial for that :/.
     
  21. Steev98

    Steev98

    Joined:
    Jun 16, 2017
    Posts:
    16
    Thank you for the help too :).
     
  22. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You're welcome. Glad it's kinda working. Just give yourself a bit of time to learn. You might like to try some tutorials here on the Unity website (in the Learn section). They might differ a bit from what you're doing, but you can probably learn a lot and then put it into anything you want to make. Or, you can just look for specific answers as you go - it's really up to you. Best wishes with your game ;)
     
    Steev98 likes this.
  23. Steev98

    Steev98

    Joined:
    Jun 16, 2017
    Posts:
    16

    I´m almost finished with this game only the thing with the lives isn´t working when i fix this, then i can release it :) but there are no tutorials for this , i want the player to have 30 lives when he falls down he loses 1 and when he has 0 the game ends and on mobile u can watch ADs to get more lives, but I don´t know how to do it like that.
     
  24. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, it looks like you already have the lives. Just begin with 30 and all should progress nicely. As far as ads go, you'll have to look that up - I've never used them, but I'm sure there are tutorials around for that.
     
  25. Steev98

    Steev98

    Joined:
    Jun 16, 2017
    Posts:
    16
    I just finished it without the lives for pc and when i convert it to android i will try to fix it :).

    Heres the link if you want to see the result, its just a small game but i made in in 4 days.

    https://steev98.itch.io/jumpobot-the-jump-run
     
  26. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool, the link says nothing is there.. ;)
    Give me 4 days and I will send you my "nothing is there" link ;)
    just kidding around. heh.
     
  27. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Oh hey, even though I couldn't see it. Congrats on your game :)
     
  28. Steev98

    Steev98

    Joined:
    Jun 16, 2017
    Posts:
    16
    What do you mean with couldn´t see it? Is the link broken?
     
  29. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I saw it now. The link was broken when I first tried, but it's working now. Looks cool :)