Hi! I have a problem with assigning color information from an image to a grid of primitives. I want to rebuild the image with primitives to use the third dimension for storing information. It will be for data vis purposes.
// Error-Log: NullReferenceException: Object reference not set to an instance of an object
DrawPrimitives.Start () (at Assets/nuilab/Scripts/DrawPrimitives.cs:13)
I am relatively new to Unity, C# and coding in general. I handled NullReferenceExeptions by myself until now. I do not find the problem. I hope that someone can lend me his or her eyes to track the issue.
Here is the first script which grabs the color information:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GetTextureColors: MonoBehaviour
{
public Texture2D Tex;
public int skip = 10;
Color[] AllColors;
//-------------------------------------------------------------------
void Start()
{
List<Color> ListOfColors = new List<Color>();
for (int x = 0; x < Tex.width; x += skip)
{
for (int z = 0; z < Tex.height; z++)
{
int currentPixel = x + z * Tex.width;
Color currentColor = Tex.GetPixel(x, z);
ListOfColors.Add(currentColor);
}
}
AllColors = ListOfColors.ToArray();
}
//-------------------------------------------------------------------
public Color[] GetColors()
{
return AllColors;
}
}
Here is the second script that builds all primitives:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DrawPrimitives: MonoBehaviour {
GetTextureColors GetTextureColorsScript;
Texture2D Texture;
Color[] currentColor;
//-------------------------------------------------------------------
void Start()
{
currentColor = GetTextureColorsScript.GetColors();
GetTextureColorsScript = GetComponent<ChangeTexture>();
Texture = GetTextureColorsScript.Tex;
GeneratePrimitives();
}
//-------------------------------------------------------------------
void GeneratePrimitives()
{
int skip = ChangeTextureScript.skip;
for (int x = 0; x < Texture.width; x += skip)
{
for (int z = 0; z < Texture.height; z += skip)
{
int currentPixel = x + z * Texture.width;
Vector3 vertex = new Vector3(x * 0.1f, 0, z * 0.1f);
GameObject Point = GameObject.CreatePrimitive(PrimitiveType.Cube);
Point.transform.position = vertex;
Point.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
Renderer rend = GetComponent<Renderer>();
rend.sharedMaterial.shader = Shader.Find("Cube_Material");
rend.sharedMaterial.SetColor("_Color" + currentPixel.ToString(), currentColor[currentPixel]);
}
}
}
}
Did you attach GetTextureColors script to the same object that uses your method? Because if you didn’t, your variant GetTextureColorsScript is null.
I found the problem. I was accessing the render component of the current gameObject instead of the instance gameObjects.
Old code:
Renderer rend = GetComponent<Renderer>();
rend.sharedMaterial.SetColor("_Color" + currentPixel.ToString(), currentColor[currentPixel]);
New code:
Renderer rend = Point.GetComponent<Renderer>();
rend.material.color = currentColor[currentPixel];