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
  4. Dismiss Notice

Object reference not set to an instance of an object Error

Discussion in 'Scripting' started by RandomBinaries, Feb 21, 2021.

  1. RandomBinaries

    RandomBinaries

    Joined:
    Oct 2, 2017
    Posts:
    5
    Hi there,

    Can anybody help me with the following error that I keep on getting in the console, which is Object reference not set to an instance of an object. It appears when I run the following code:

    var SCORE : UnityEngine.UI.Text;
    var Show_SCORE : Transform;
    var gameOBJECT_score_SCRIPT : Increment_SAVE_Show_SCORE = Show_SCORE.GetComponent(Increment_SAVE_Show_SCORE);

    function Update () {
    SCORE.text = "SCORE: " + gameOBJECT_score_SCRIPT.SAVED_money.ToString();
    }

    Help much appreciated thanks.
     
  2. reachdabeach

    reachdabeach

    Joined:
    Jan 28, 2021
    Posts:
    70
    I don't see SCORE getting set to a value. Maybe in Start you are getting the component for it? IF not, you likely should be.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    The answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.
     
    reachdabeach likes this.
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,742
    I'd like to back up a second and ask why you're using Javascript in Unity in 2021?
     
  5. RandomBinaries

    RandomBinaries

    Joined:
    Oct 2, 2017
    Posts:
    5
    I'm using Unity version 2017. My preference as it supports more languages than only C# with 2021 version
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,742
    To rephrase the question, why are you so dedicated to using an objectively worse programming language that you're letting it hold you back from getting the bugfixes and new features of later versions?

    If you switch to C#, you'd not only get the benefits of a more capable language, you'd also get the benefits of a modern version of Unity as well as the benefits of a language that is better supported by tools like Visual Studio, as well as the benefits of a language that people still actually use.

    The linked article above was written 4 years ago when learning JS in Unity was already an objective waste of effort compared to C#; it has only become more wasteful every year that has gone past.
     
    Last edited: Feb 22, 2021
    Kurt-Dekker likes this.
  7. reachdabeach

    reachdabeach

    Joined:
    Jan 28, 2021
    Posts:
    70
    On rare occasions you will drop into a rabbit hole after step 2, resulting in steps 3-25 (step 17 is seeking counseling)...

    Echoing StarManta's suggestion, the C# scripting integration in VS (if you are on Windows) is really nice. Then you will seek help only if the why isn't simple and/or obvious when you look at it in the debugger. Another dev I have done some work with is using JetBrains Raider C# on the Mac and has similar capability. Not sure what sort of debug interface you have with JS. It's really not bad hopping between the C like object languages.
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Oh yeah, my home address is in those rabbit holes. I love it here! All my favorite exceptions visit here regularly! We don't even wear try/catch/finally masks when we hang out... fearless!
     
    StarManta likes this.
  9. RandomBinaries

    RandomBinaries

    Joined:
    Oct 2, 2017
    Posts:
    5
    I thought this forum was for people who needed help & not questioning people's choice of what programming language they use. It's an elitist way of thinking. The more choice the more power you hold as a individual/consumer to shape how things work. Now whether I want to work in JavaScript is my choice. Whether you want to work in C# is your choice, but we can both meet in the middle & say that any programming follows the same principle of if statements, for loop, classes, function & methods. Lets just leave it at that.
     
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,742
    That's what's happening here. If you see someone with a shotgun aimed at their foot, then advising them to put the shotgun down is helping. I suppose you could make an argument that it's elitist to think that "not blasting off your foot" is better than "blasting off your foot", in which case... fine, I'm elitist?
     
    Kurt-Dekker likes this.