- Home /
The question is answered, right answer was accepted
Error CS0029 Help? (Screenshot of Exact Error)
using UnityEngine;
using System.Collections;
public class ChangeMaterial : MonoBehaviour
{
private Renderer rend;
private Shader differentShader;
public int[,] materials = new int [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30];
void Start ()
{
differentShader = Shader.Find ("Toon/Lit Outline");
rend = GetComponent<Renderer> ();
rend.enabled = true;
rend.sharedMaterial = materials[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30];
for (int i = 0; i < materials.Length; i++)
{
rend.sharedMaterial.shader = differentShader;
}
}
}
I created this "for" loop earlier to change the Shader of all 30 elements (which are materials) at the beginning of the scene. I am using an array as opposed to a list because the indexes will not change. The error is in line 10. What am I doing wrong?
@stephen_george98 Your syntax is incorrect. See $$anonymous$$ultidimensional Arrays (C# Program$$anonymous$$g Guide)
Answer by Namey5 · Oct 27, 2016 at 09:31 PM
You are declaring a two-dimensional array, yet assigning it an apparent 31-dimensional array. The materials array in a renderer is a one-dimensional array, and as such does not need any commas. Not to mention, you are assigning an integer value to a material variable. You are also not actually doing anything in the for loop, just simply repeating the exact same line 31 times. The following is how you would go about doing this;
using UnityEngine;
using System.Collections;
public class ChangeMaterial : MonoBehaviour
{
private Renderer rend;
private Shader differentShader;
void Start ()
{
differentShader = Shader.Find ("Toon/Lit Outline");
rend = GetComponent<Renderer> ();
rend.enabled = true;
for (int i = 0; i < rend.sharedMaterials.Length; i++)
{
rend.sharedMaterials[i].shader = differentShader;
}
}
}
Wow thank you for pointing out that I was actually creating a 31 dimensional array, my thinking was totally backward. And I get a NullReferenceException whenever I use your code. That is because I cannot now set the 30 Array elements (30 materials) in the Inspector.
That is why in my code (although not the greatest) I had line 20 and set the 30 materials in the Array. Is there a better way of doing that instruction that you know of?
So you need to access each material individually? In which case you would use a material array, i.e.
using UnityEngine;
using System.Collections;
public class Change$$anonymous$$aterial : $$anonymous$$onoBehaviour
{
private Renderer rend;
private Shader differentShader;
public $$anonymous$$aterial[] materials = new $$anonymous$$aterial[31];
void Start ()
{
differentShader = Shader.Find ("Toon/Lit Outline");
rend = GetComponent<Renderer> ();
rend.enabled = true;
for (int i = 0; i < materials.Length; i++)
{
materials[i].shader = differentShader;
}
rend.shared$$anonymous$$aterials = materials;
}
}
Works like a charm! Thank you and thank you for $$anonymous$$ching me something new today. I accepted your answer
Follow this Question
Related Questions
C# array not behaving as expected 0 Answers
Finding a pattern in an array 1 Answer
Affect every object in array. 1 Answer
Distribute terrain in zones 3 Answers
Object reference not set to an instance of an object 1 Answer