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

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();

}

// 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!

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.

2 Likes

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! :slight_smile:

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!

6207189--681735--Screenshot 2020-08-15 at 14.29.23.png

Can you include a screenshot of the entire GameObject and all its components?

2 Likes

Here are the Screenshots :):

In the ColorPicker script, which one is line 36?

It is this one:

CurrMat = renderer.material;

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.

1 Like

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.

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?

1 Like

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.

2 Likes

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?

1 Like

Here is the debug:

6210449--682418--Screenshot 2020-08-16 at 20.10.13.png

It seems that renderer is the problem even though I have a mesh renderer. Hmm…

I’m totally lost.

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.

1 Like

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.

1 Like

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.

1 Like

The GameObject’s material is attached to the Mesh Renderer so I guess that’s not it.

The only places the script is attached is to the UI Button and my Player Prefab.

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!