- Home /
Assign image color values to multiple primitives
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]);
}
}
}
}
Answer by rettichmann · Dec 21, 2016 at 03:03 PM
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];
Answer by lamphung719 · Dec 21, 2016 at 09:16 AM
Did you attach GetTextureColors script to the same object that uses your method? Because if you didn't, your variant GetTextureColorsScript is null.
Thank you for your reply!
Here is my GameObject with attached scripts: Link to Inspector Screenshot
change the order of code
currentColor = GetTextureColorsScript.GetColors();
GetTextureColorsScript = GetComponent<ChangeTexture>();
to
GetTextureColorsScript = GetComponent<ChangeTexture>();
currentColor = GetTextureColorsScript.GetColors();
because you have not assign GetTextureColorsScript to your variant, so you got null exception
Sorry for the different na$$anonymous$$g: GetTextureColorsScript = GetComponent(); It is actually: ChangeTextureScript = GetComponent();
That is a good hint! Thank you! Unfortunately a new Exeption appears.
NullReferenceException: Object reference not set to an instance of an object DrawPrimitives.GeneratePrimitives () (at Assets/nuilab/Scripts/DrawPrimitives.cs:35) DrawPrimitives.Start () (at Assets/nuilab/Scripts/DrawPrimitives.cs:18)
With the help of @lamphung719 I managed to get rid of the error message. Unfortunately a new one popped up.
Error-Log: NullReferenceException: Object reference not set to an instance of an object DrawPrimitives.GeneratePrimitives () (at Assets/nuilab/Scripts/DrawPrimitives.cs:35) DrawPrimitives.Start () (at Assets/nuilab/Scripts/DrawPrimitives.cs:18)
DrawPrimitives.cs:18
GeneratePrimitives();
DrawPrimitives.cs:35
rend.shared$$anonymous$$aterial.shader = Shader.Find("Cube_$$anonymous$$aterial");
Your answer
![](https://koobas.hobune.stream/wayback/20220612103105im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to avoid color multiplication with UI.Graphic.CrossFadeColor? 1 Answer
How to change the color of an image every two seconds 1 Answer
Using Texture2D.GetPixels() to take a screenshot and then show it on an Image - iOS problems. 1 Answer
Image UI color not changing when sprite is set via script 0 Answers
How can I change color of an Image in script (png source) ? 1 Answer