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

NullReferenceException: Object reference not set to an instance of an object. PLS HELP ME!!!!!

Discussion in 'Editor & General Support' started by Dandev_Industries, Aug 14, 2020.

  1. Dandev_Industries

    Dandev_Industries

    Joined:
    Aug 14, 2020
    Posts:
    11
    I just started working with unity. I was making a color selection screen where you press different buttons to change to colour of the main character (Cube) and I got this error.

    This is the exact error:

    NullReferenceException: Object reference not set to an instance of an object
    ColorPicker.RedColor () (at Assets/Scripts/ColorPicker.cs:36)

    And this is my color selection code:

    ------------
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class ColorPicker : MonoBehaviour
    {

    public Material[] BodyColorMat;
    Material CurrMat;
    Renderer renderer;

    // Use this for initialization
    void Start()
    {

    renderer = this.GetComponent<Renderer>();

    }

    // Update is called once per frame
    void Update()
    {

    }

    public void BlueColor()
    {
    CurrMat = renderer.material;
    renderer.material = BodyColorMat[0];
    }

    public void RedColor()
    {
    CurrMat = renderer.material;
    renderer.material = BodyColorMat[1];
    }

    public void GreenColor()
    {
    CurrMat = renderer.material;
    renderer.material = BodyColorMat[2];
    }

    public void YellowColor()
    {
    CurrMat = renderer.material;
    renderer.material = BodyColorMat[3];
    }
    }
    ------------

    pls help me. Thanks!
     
  2. seraphki

    seraphki

    Joined:
    Jul 20, 2016
    Posts:
    26
    It sounds like 'renderer' is null - make sure you have a Renderer component on the same GameObject as this script, otherwise the GetComponent in your Start() will fail and you'll get a null reference. If the Renderer is on a child object, you'll need to use GetComponentInChildren, or just make the Renderer field public and assign it manually in the inspector.
     
  3. Dandev_Industries

    Dandev_Industries

    Joined:
    Aug 14, 2020
    Posts:
    11
    The GameObject has a Mesh Renderer and its not a child object. Is there another thing you might know that could cause the problem? Because I'm not really sure what is causing the problem. Thank you! :)

    Let me also give some more context:
    I made it so that the GameObject (Player) changes colors when I press one of the UI Buttons, and that is when the error occurs. Here is also an attached picture of the script in the GameObject. Thank You again!
     

    Attached Files:

  4. seraphki

    seraphki

    Joined:
    Jul 20, 2016
    Posts:
    26
    Can you include a screenshot of the entire GameObject and all its components?
     
  5. Dandev_Industries

    Dandev_Industries

    Joined:
    Aug 14, 2020
    Posts:
    11
    Here are the Screenshots :):

    Screenshot 2020-08-16 at 12.24.59.png Screenshot 2020-08-16 at 12.25.35.png
     
  6. Raidenwins

    Raidenwins

    Joined:
    Dec 18, 2012
    Posts:
    132
    In the ColorPicker script, which one is line 36?
     
  7. Dandev_Industries

    Dandev_Industries

    Joined:
    Aug 14, 2020
    Posts:
    11
    It is this one:

    CurrMat = renderer.material;
     
  8. Raidenwins

    Raidenwins

    Joined:
    Dec 18, 2012
    Posts:
    132
    Are familiar with debugging C# code? If so, put a breakpoint on line 36, attach your editor (most likely Visual Studio) to Unity and then attempt to change the color of you main character. When your debugger stops on line 36, hover with your mouse over CurrMatt, renderer, and then material to see which one exactly is null. Let us know what you find out or if you have any questions about debugging.
     
    Dandev_Industries likes this.
  9. Dandev_Industries

    Dandev_Industries

    Joined:
    Aug 14, 2020
    Posts:
    11
    I am not sure if I debugged it correctly but when I did I got this:

    'ColorPicker.renderer' hides inherited member 'Component.renderer'. Use the new keyword if hiding was intended.
     
  10. Raidenwins

    Raidenwins

    Joined:
    Dec 18, 2012
    Posts:
    132
    I don't think that's the right debug message to show us what is null. When the debugger hits the breakpoint on line 36, can you hover over CurrMatt, take a screenshot, and then post here?
     
    Dandev_Industries likes this.
  11. seraphki

    seraphki

    Joined:
    Jul 20, 2016
    Posts:
    26
    I'm not able to see all the components for Player here - I'm trying to ensure that the same GameObject that has the ColorPicker component also has a Renderer component.
     
  12. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    It seems to, you can see the MeshRenderer right underneath the color picker. The renderer has no material but given the code presented here that shouldn't cause a null ref... I wonder if there are multiple of this script present in the scene?
     
    Dandev_Industries likes this.
  13. Dandev_Industries

    Dandev_Industries

    Joined:
    Aug 14, 2020
    Posts:
    11
    Here is the debug:
     

    Attached Files:

  14. Dandev_Industries

    Dandev_Industries

    Joined:
    Aug 14, 2020
    Posts:
    11
    It seems that renderer is the problem even though I have a mesh renderer. Hmm...

    I'm totally lost.
     
    Last edited: Aug 17, 2020
  15. seraphki

    seraphki

    Joined:
    Jul 20, 2016
    Posts:
    26
    Whoops! Somehow I missed the second picture. yeah I'm guessing its trying to pull the material and getting a null ref because there is no material assigned to your mesh renderer. I'd try assigning a material and giving that a go.
     
    Dandev_Industries likes this.
  16. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    If it's really the renderer that is null, your most likely issue is you have another copy of this script somewhere in the scene that doesn't have a renderer attached.
     
    Dandev_Industries likes this.
  17. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Your player object is disabled.

    Scripts on a disabled objects won't have their Start() function called until they have been enabled. Therefore, your code in Start() that gets the renderer component will never execute. You can verify this by adding a Debug.Log statement in the start function.

    Enable your player, or make "renderer" public/serializable and drag-and-drop the renderer component into the field.
     
    Dandev_Industries likes this.
  18. Dandev_Industries

    Dandev_Industries

    Joined:
    Aug 14, 2020
    Posts:
    11
    The GameObject's material is attached to the Mesh Renderer so I guess that's not it.
     
  19. Dandev_Industries

    Dandev_Industries

    Joined:
    Aug 14, 2020
    Posts:
    11
    The only places the script is attached is to the UI Button and my Player Prefab.
     
  20. Dandev_Industries

    Dandev_Industries

    Joined:
    Aug 14, 2020
    Posts:
    11
    I am not very sure how to enable my player. (As I am a beginner and total trash at this). Could you tell me how I should do that. Thanks!
     
  21. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    There's a checkbox next to the name of the object at the top of the inspector. Enable it.
     
    Dandev_Industries likes this.
  22. Dandev_Industries

    Dandev_Industries

    Joined:
    Aug 14, 2020
    Posts:
    11
    I only unchecked it on that scene because I didn't want it to be visible, but even though I checked it now it still has the same problem.
     
  23. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    It's time to add debug.log statements absolutely everywhere.

    Add one in start after assigning to renderer, print the value of renderer to verify that it is not null. Add debug.log statements to all your public functions to verify that "renderer" is not null etc
     
    Dandev_Industries likes this.