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
1
Question by chris3331 · Jun 09, 2015 at 09:52 PM · materialmaterialsreplacesharedmaterial

Replacing sharedMaterials by an existing Material C#

Hello, My question is why this is working and changing the color:

 gameObject.GetComponent<Renderer> ().sharedMaterials[1].color = Color.blue;

But replaced by this it isn´t changing the material:

 gameObject.GetComponent<Renderer> ().sharedMaterials[1] = Resources.Load ("NewMaterial/TestMat") as Material;

or this;

 Material NewMat = new Material (Resources.Load ("NewMaterial/TestMat") as Material);
 Debug.Log (NewMat); // working!!
 gameObject.GetComponent<Renderer> ().sharedMaterials[1] = NewMat;

I don´t get an error and the material "TestMat" is stored in the right folder! As an Unity-Beginner i probably overlooked something obvious! Thanks for helping!!!


for better understanding

 using UnityEngine;
 using System.Collections;
 
 public class SetMaterialToo_04 : MonoBehaviour 
 {
     
     void Start() {
 
         Material[] materials = gameObject.GetComponent<Renderer> ().sharedMaterials;
         int Arraylength = materials.Length;
         Debug.Log (Arraylength);
 
         for (int i = 0; i < Arraylength; i++) {
             string matName = materials [i].name;
         
 
             if (matName == "Test_rot") { 
 
                 int var = i;
                 Debug.Log (materials [var]);
 
                 Material NewMat = new Material (Resources.Load ("NewMaterial/TestMat") as Material);
                 Debug.Log (NewMat); // working!!
                 gameObject.GetComponent<Renderer> ().sharedMaterials [1] = NewMat;
 
             
             }
         }
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by maccabbe · Jun 10, 2015 at 02:23 AM

gameObject.GetComponent ().sharedMaterials and gameObject.GetComponent ().materials return a copy of the array that stores the materials.

 mesh.materials = {mat1}
 copyMaterials = mesh.materials
 so
 copyMaterials = {mat1}

So we do not expect that editing the copy to affect the original.

 copyMaterials = {newMat}
 but
 mesh.materials = {mat1}

In addition, the copy of the array of materials stores the original materials. So editing the materials contained in the copy of the array also edits the materials contained in the original array

 copyMaterials.mat1.color=newColor
 so
 mesh.materials = {mat1} (where mat1 has had a new color assigned)

However you can change mesh.materials[] by assigning to it

 Material[] materials=gameObject.GetComponent<Renderer> ().sharedMaterials;
 materials[1]=Resources.Load ("NewMaterial/TestMat") as Material;
 gameObject.GetComponent<Renderer> ().sharedMaterials=materials;




Comment
Add comment · Show 2 · 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 chris3331 · Jun 11, 2015 at 11:52 AM 0
Share

Thank you very much! But what if I want to keep the changed materials even after ending the Play $$anonymous$$ode? At the moment all changes are resetting after ending the Play $$anonymous$$ode but I would like to change the existing materials of the Object forever!

avatar image OnyDeus · Jun 26, 2015 at 01:18 AM 1
Share

I beleive if you want the changes to be permanent you should look into [ExecuteInEdit$$anonymous$$ode] (and thank you for asking I was banging my head and should have known better)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Material vs. SharedMaterial - difference in SETTING? 1 Answer

Keep replaced materials of a Game Object even after ending the Play Mode C# 0 Answers

How to share material between meshes 1 Answer

How to apply a color to the second texture in this shader? 1 Answer

Can't find unlit shader in material shader selection!, 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