- Home /
What's wrong with this array assignment?
Hey,
this is a pretty simple piece of code and I have no idea why it wouldn't work.
for(int i=0;i<tempArray.Length;++i)
{
m_PrevAlphas.Add(tempArray[i].materials);
}
for (int i=0; i<tempArray.Length; ++i)
{
Color newColor = new Color (1.0f, 1.0f, 1.0f, m_TransparentAmount);
for(int j=0;j<tempArray[i].materials.Length;++j)
{
Texture tempTexture=tempArray[i].materials[j].GetTexture("_MainTex");
tempArray[i].materials[j]=m_Mat; //THIS GETS IGNORED
tempArray[i].materials[j].SetTexture("_MainTex",tempTexture);
tempArray[i].materials[j].color=newColor;
}
}
Any idea why it wouldn't get the Mat in tempArray[i].materials[j]=m_Mat to my m_Mat? I checked the value of m_Mat and it is def valid, and there is already a material in that array that I can see so I know there is space in memory for it.
also did this.
$$anonymous$$aterial m=tempArray[i].materials[j]; m=m_$$anonymous$$at
This assigns m to m_$$anonymous$$at successfully but still doesn't change the material in the array even though its a reference.
Test to write in current context the name āJā
for (int j = 0; j < TempArray.Longueur; ++j){
Answer by omg_Aya · Jan 02, 2015 at 05:06 PM
Ok found the answer.
http://answers.unity3d.com/questions/8993/change-material-of-an-object.html
Basically, you can't re-assign values in the array, but you can re-assign the entire array. Should of thought of that earlier with how C# works.
Your answer
![](https://koobas.hobune.stream/wayback/20220613174446im_/https://answers.unity.com/themes/thub/images/avi.jpg)