- Home /
NullReferenceException in an array
NullReferenceException has brought me to my knees once more in the following script:
using UnityEngine; using System.Collections;
public class ColorPicker : MonoBehaviour {
private Texture2D[] currentColors;
private GameObject[] currentCubes;
Texture2D currentColorTexture;
void Start() {
currentColorTexture= new Texture2D(1,1);
}
void OnGUI() {
for (int aY = 0;aY<currentColors.Length-1;aY++) {
currentColorTexture = currentColors[aY];
GUIStyle currentColorStyle = new GUIStyle();
currentColorStyle.normal.background = currentColorTexture;
if(GUI.Button(new Rect(10,(aY*10) + 10, 10,50), currentColors[aY], ""))
Debug.Log("ah");
}
}
public void UpdateColors() {
currentCubes = GameObject.FindGameObjectsWithTag("Cube");
currentColors = new Texture2D[currentCubes.Length];
for(int aA = 0;aA<currentColors.Length;aA++) {
currentColors[aA] = new Texture2D(1,1);
currentColors[aA].SetPixel((int)0,(int)0, currentCubes[aA].renderer.material.color);
}
for (int aB = 0;aB<currentCubes.Length;aB++) {
for (int aC = 0;aC<currentColors.Length;aC++) {
currentColorTexture.SetPixel(0,0, currentCubes[aB].renderer.material.color);
currentColors[aB] = currentColorTexture;
}
}
}
}
I've literally spent six and half hours trying to find the error in this simple piece of work but it keeps giving me the high-blood-pressure-inducing NullReferenceException.
I would deeply appreciate it if somebody could explain what I'm doing wrong here.
Thanks, Elliot Bonneville
In the OnGui() ForLoop, your end condition is '`array.Length-1`', but in the UpdateColors() ForLoop, your end condition is array.Length
... is this intentional?
No, but it shouldn't cause a problem. I was in the process of deter$$anonymous$$ing which one worked, and forgot to change the OnGUI loop. Thanks for pointing that out, though.
Answer by tertle · Feb 14, 2011 at 11:46 PM
At a first glance.
currentCubes = GameObject.FindGameObjectsWithTag("Cube");
currentColors = new Texture2D[currentCubes.Length];
Could potentially cause a NullReferenceException if nothing with a Cube tag exists.
currentCubes = GameObject.FindGameObjectsWithTag("Cube");
if (currentCubes != null)
{
currentColors = new Texture2D[currentCubes.Length];
...
}
Should fix that.
Not sure if that's your specific issue though.
-edit-
also
void OnGUI() {
for (int aY = 0;aY<currentColors.Length-1;aY++) {
If it runs before
public void UpdateColors()
Which I'm not sure when you call, will cause a NullReferenceException.
Should initialize it to 0.
private Texture2D[] currentColors = new Texture2D[0];
Or check if it's null before the for loop. Or call updatecolours from start() etc.
Nope, I most definitely have objects tagged Cube in my scene. Thanks for replying, though. :)
I posted the same as your updated answer in a comment on the question, but have deleted it - upvote ins$$anonymous$$d!
Calling UpdateColors() from Start seems to have partially solved the problem. Thanks!
Your answer
Follow this Question
Related Questions
How to show an array of custom objects in Inspector? 2 Answers
Unity Object Array C# 2 Answers
Setting parent gives error? 2 Answers
Pushing textures in an Array 2 Answers
Problem with Singleton and NullReferenceException? 0 Answers