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()
{
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.
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!
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.
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?
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.
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?
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.
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.
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.