Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by GDI_Commando · Jul 11, 2013 at 08:46 PM · errormaterialmaterialsrendererrender

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GDI_Commando · Jul 13, 2013 at 09:22 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing two different objects renderer colour 1 Answer

Emissive Material control via script (code) 1 Answer

Instantiating Material Error? 0 Answers

How to compare 2 materials? 2 Answers

How to check whether a lot of components and their children have a mesh renderer on them in code? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges