- Home /
Storing object colors in an array.
I'm trying to store the colors of objects found in array named "components[]" in array "originalColor[]" before changing the colors with OnMouseOver(). I'd like to then change them back to originalColor[] with OnMouseExit(). The below code is receiving "Object reference not set to an instance of an object" on the original Color line. Coding in C#. Please help.
public GameObject[] components; Color[] originalColor;
for (int i = 0; i < components.Length; i++)
originalColor[i] = components[i].renderer.material.color;
Yes, it is public and filled with 20 objects in the inspector currently.
Answer by fafase · Feb 21, 2013 at 08:37 PM
originalCOlor is just a pointer to array of Color but is not pointing anywhere. You need to instantiate the array to store the values.
public GameObject[] components;
Color[] originalColor;
void Start(){
originalColor = new Color[components.Length];
for (int i = 0; i < components.Length; i++)
originalColor[i] = components[i].renderer.material.color;
}
That did it! Here is the working code so far! Thanks!
public GameObject[] components;
Color[] originalColor;
void Start()
{
originalColor = new Color[components.Length];
for (int i = 0; i < components.Length; i++)
{
originalColor[i] = components[i].renderer.material.color;
}
}
void Update()
{
}
void On$$anonymous$$ouseOver()
{
for (int i = 0; i < components.Length; i++)
components[i].gameObject.renderer.material.color = Sections$$anonymous$$anager.Instance.HoverColor;
}
void On$$anonymous$$ouseExit()
{
for (int i = 0; i < components.Length; i++)
components[i].renderer.material.color = originalColor[i];
}
}
Your answer
Follow this Question
Related Questions
Adding little delays between doing things to objects in array 2 Answers
How to make an Array equal the Components of an Instantiated GameObject 1 Answer
Array of variable-length arrays (JS) 1 Answer
How to make a custom type's contents modifiable in an array by inspector? 1 Answer
whats the best way to store a large array of vector3 of integers 1 Answer