Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Oct 27, 2016 at 10:32 PM by stephen_george98 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by stephen_george98 · Oct 27, 2016 at 09:24 PM · c#errorarrayfor-loopindex

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;
         }
     }
 }

Error Screenshot

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?

screen-shot-2016-10-27-at-41507-pm.png (11.5 kB)
Comment
Add comment · Show 1
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 Sergio7888 · Oct 27, 2016 at 09:36 PM 0
Share

@stephen_george98 Your syntax is incorrect. See $$anonymous$$ultidimensional Arrays (C# Program$$anonymous$$g Guide)

1 Reply

  • Sort: 
avatar image
2
Best Answer

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;
          }
      }
  }
Comment
Add comment · Show 4 · 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 stephen_george98 · Oct 27, 2016 at 10:08 PM 0
Share

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.

avatar image stephen_george98 · Oct 27, 2016 at 10:09 PM 0
Share

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?

avatar image Namey5 stephen_george98 · Oct 27, 2016 at 10:14 PM 1
Share

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;
           }
       }
avatar image stephen_george98 Namey5 · Oct 27, 2016 at 10:32 PM 0
Share

Works like a charm! Thank you and thank you for $$anonymous$$ching me something new today. I accepted your answer

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

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


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