Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Error

Discussion in 'Scripting' started by x267Unity, May 11, 2024.

  1. x267Unity

    x267Unity

    Joined:
    May 5, 2024
    Posts:
    5
    NullReferenceException: Object reference not set to an instance of an object
    what does this error mean?

    objectDisplay.ViewObject(transform.gameObject, treeHealth, MaxTreeHealth, 0.25f);
    the line with the function call

    public void ViewObject(GameObject displayedObject, int objectHealth, int objectMaxHealth, float objectResurs)
    the line with the function declaration
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,162
    it means some variable you used didnt get set to anything so it couldnt do it... in the case likely objectDisplay
     
    dstears likes this.
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,665
    A variable or field on that line currently has a value of
    null
    .
     
  4. x267Unity

    x267Unity

    Joined:
    May 5, 2024
    Posts:
    5
    I understand this, but as a transform.The GameObject can be empty if it is the object to which the script is attached
     
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,665
    It's most likely not the transform. We'd have to see actual code to be able to help more.
     
  6. JeffreyCurtis

    JeffreyCurtis

    Joined:
    Jun 4, 2024
    Posts:
    1
    I am having a doubt, can I ask a question?
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,316
    Please don't necro-post. If you have a new question, make a new post. It's FREE!!


    If all you have is a null reference, you don't need to post because:

    The answer is always the same... ALWAYS!

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that


    If you have an actual question, when you post, remember we cannot read your mind and we cannot see your screen.

    How to report your problem productively in the Unity3D forums:

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

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?


    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/