- Home /
Index array is out of range (following H&S tut)
I have been trying to get this code to work for a while now, I am pretty new to this whole C# scripting and never really finished learning XML. My problem is with changing a material for the hair. I set up a specific mesh slot for the hair and somehow it just wont work. It constantly give me the "Index array is out of range" error. In the "Changing Room" scene it will change the hair colour but spews out the error constantly, in the "CharacterCustomization" scene it wont load any materials.
This is the code that is causing the problem
private void ChangeHairMaterialGUI() {
if(GUI.Button(new Rect(Screen.width * .5f - 95, Screen.height - 175, 30, 30), "<"))
{
_hairMaterialIndex--;
ChangeHairMeshMaterial(CharacterMeshMaterial.Hair);
}
GUI.Box(new Rect(Screen.width / 2 - 60, Screen.height - 175, 120, 30), _hairMaterialIndex.ToString());
if(GUI.Button(new Rect(Screen.width * .5f + 65, Screen.height - 175, 30, 30), ">"))
{
_hairMaterialIndex++;
ChangeHairMeshMaterial(CharacterMeshMaterial.Hair);
}
}
private void ChangeHairMeshMaterial(CharacterMeshMaterial cmm) {
Material[] mats = pc.hairMaterialMesh.renderer.materials;
for(int cnt = 0; cnt < ca.hairMaterial.Length; cnt++)
mats[cnt] = ca.hairMaterial[cnt];
//switch(cmm) {
// case CharacterMeshMaterial.Hair:
if(_hairMaterialIndex > ca.hairMaterial.Length - 1)
_hairMaterialIndex = 0;
else if(_hairMaterialIndex < 0)
_hairMaterialIndex = ca.hairMaterial.Length - 1;
mats[(int)cmm] = ca.hairMaterial[_hairMaterialIndex];
// break;
//}
DestroyImmediate(pc.hairMaterialMesh.renderer.materials[(int)cmm]);
pc.hairMaterialMesh.renderer.materials = mats;
// Debug.Log(mats.Length);
// Debug.Log("Wearing:" +pc.characterMaterialMesh.renderer.materials[2].name + " Should be wearing: " + ca.torsoMaterial[_torsoMaterialIndex].name );
// Resources.UnloadUnusedAssets();
}
The main problem is in the line
mats[(int)cmm] = ca.hairMaterial[_hairMaterialIndex];
Any help would be greatly appreciated as I have big plans in the works!
Answer by acemuzzy · Jul 11, 2013 at 09:04 PM
"Index array is out of range means" one of your two array indices on that line is negative or >= length.
It can't be the rhs, given your defensive code above.
So what is (int)cmm and what is mats.length? That must be the issue. Can you force (int)cmm to be in range in a similar way? Why would it be out of range?
I'm not sure I follow.
It's defined in Material[] mats = pc.hairMaterialMesh.renderer.materials
Yes, that's how mats is defined, but it doesn't answer what its length is, as it depends what the length of pc.hairMaterialMesh.renderer.materials is. Can you add a debug line to answer that question definitively?
Debug.Log("Length of mats is " + mats.Length);
Also print (int)cmm:
Debug.Log("(int)cmm is " + (int)cmm);
Then compare the two.
I have tried using "ca.hairMaterial" with both [cnt] and .Length appendices but it then throws up that it isn't assigned to an object
Where have you tried that? Line 25 has ca.hairMaterial.Length, was it valid there?
Does e.g. the following even work?
mats[0] = ca.hairMaterial[_hairMaterialIndex];
I'm not familiar with those tutorials, but I'm guessing that you have different resources loaded in your scene, which is why some of the arrays you reference are of different lengths. You just need to me methodical in terms of tracking down exactly what's going on.
I have tried a couple of changes now. "mats[0] = ca.hair$$anonymous$$aterial[_hair$$anonymous$$aterialIndex];" does nothing other than load the first hair colour.
mats.Length is always 1 and (int)cmm is always equal to 0 even if I change the _hair$$anonymous$$aterialIndex to 8.
Answer by GDI_Commando · Jul 12, 2013 at 10:43 AM
It's defined in Material[] mats = pc.hairMaterialMesh.renderer.materials; I have tried using "ca.hairMaterial" with both [cnt] and .Length appendices but it then throws up that it isn't assigned to an object. I copied it directly from Petey's "ChangeTorsoMaterial" settings. I'm not sure if changing the value on one of the mats definitions to 8 (number of my materials) would work or not.