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 Giantbean · Nov 07, 2013 at 10:27 PM · arraymaterialsrenderelement

type ...Material[] vs Material (materials change at run time)

I am trying to look through an array of materials on a specific game object and change the material at run time. I have read over multiple pages of the scripting reference on materials, game objects, render, transforms, Material.SetTexture and so on. I have also searched and read others answers on similar questions in the forums. All this research points to the fact that I should be using material[] to form an array which will allow me to set the material element I need to change.

However using the code below I get this: error CS0029: Cannot implicitly convert type UnityEngine.Material' to UnityEngine.Material[]'

I can not find a resolution for this error and I'm not sure how to convert the material to an array.

What am I doing wrong with my code?

 using UnityEngine;
 using System.Collections;
 
 public class ChangeMaterial : MonoBehaviour {
     
     //Put the game object with the texture you are changing here.
     GameObject Model;
     public string target;
     // put the first material here.
     public Material material1;
     // put the second material here.
     public Material material2;
     
     bool FirstMaterial = true;
     bool SecondndMaterial = false;    
     Material[] themes;
 
     
     void Start ()
     {
         
     Model = GameObject.Find (target);
     themes = Model.renderer.materials[4];
     themes[4] = material1;
     Model.renderer.materials[4] = themes; 
 
     }
      
     void OnMouseDown()
     {
         if (!SecondndMaterial)
             {
            themes[4] = material2;
            SecondndMaterial = true;
         FirstMaterial = false;
         }
      
         else if (SecondndMaterial)
         {
         themes[4] = material1;
         FirstMaterial = true;
         SecondndMaterial = false;
         }
     }
 }

 
Comment
Add comment · Show 2
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 Giantbean · Nov 07, 2013 at 10:31 PM 0
Share

P.S. if you use

 $$anonymous$$odel.renderer.material[4]

 

ins$$anonymous$$d of

 $$anonymous$$odel.renderer.materials[4]

the lack of an s changes the error to: error CS0021: Cannot apply indexing with [] to an expression of type `UnityEngine.$$anonymous$$aterial'

avatar image ArkaneX · Nov 07, 2013 at 10:33 PM 0
Share

The problem is mixing class instance with array of class instances. I suggest reading a short info on arrays in Unity Wiki. If, after reading it, you'll still have problem, please return here.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by zertach · Nov 07, 2013 at 10:36 PM

 Model = GameObject.Find(target);
 themes = Model.renderer.materials[4]; 
 themes[4] = material1;
 Model.renderer.materials[4] = themes;

themes is array and materials[4] returning single material. so you can not transfer single material to material array.

 delete all those lines and use like this. Model.renderer.materials[4] = yournewmaterial
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 Giantbean · Nov 08, 2013 at 03:58 PM 0
Share

This still is not working for me. Can the "yournewmaterial" be set with material1 and material2 in my code? I'm not getting an error in the console but the material does not change.

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

17 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 avatar image

Related Questions

getting variable from every element in array? 1 Answer

Using variables inside a GameObject[] array 1 Answer

how subtract total value element of array ?? 1 Answer

Index array is out of range (following H&S tut) 2 Answers

Getting info from object within an array 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