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 /
  • Help Room /
avatar image
0
Question by unity_BfhJRB37TPcoUg · Aug 08, 2018 at 06:40 AM · materialmaterialssubstance

How do I change a sub material with a button click?

So I have most of the code completely working at this point. I am able to change a material with the button click. However, the problem arises when I try to change element 1 instead of element 0.

alt text

This is my current code which isn’t working.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class ColourChangeShield : MonoBehaviour {

 [SerializeField]
 private Material[] materials;
 private Renderer rend;
 private int selectedMaterialIndex = 0;

 void Start()
 {
     rend = GetComponent<Renderer>();
     rend.enabled = true;
     SelectMaterial(materials[selectedMaterialIndex]);
 }

 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Alpha1))
         SelectNextMaterial();

     if (Input.GetKeyDown(KeyCode.Alpha2))
         SelectPreviousMaterial();
 }

 public void SelectNextMaterial()
 {
     selectedMaterialIndex++;
     if (selectedMaterialIndex >= materials.Length)
         selectedMaterialIndex = 0;
     Debug.Log("selectedMaterialIndex: " + selectedMaterialIndex);
     SelectMaterial(materials[selectedMaterialIndex]);
 }

 public void SelectPreviousMaterial()
 {
     selectedMaterialIndex--;
     if (selectedMaterialIndex < 0)
         selectedMaterialIndex = materials.Length - 1;
     Debug.Log("selectedMaterialIndex: " + selectedMaterialIndex);
     SelectMaterial(materials[selectedMaterialIndex]);
 }

 public void SelectMaterial(Material material)
 {
     rend.materials[2] = material;
 }

}

However, when the void SelectMaterial is structured like this it works.

 public void SelectMaterial(Material material)
 {
     rend.sharedMaterial = material;
 }

but only changes element 0 which is not the one I want to change. What can I do to make sure element 1, 2, 3 etc is changed.

Thank you in advance for any help you can provide

2.jpg (51.5 kB)
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
0
Best Answer

Answer by Hellium · Aug 08, 2018 at 11:41 AM

As indicated in the documentation, Renderer.materials returns a copy of materials array. If you want to change some materials in it, get the value, change an entry and set materials back.

  public void SelectMaterial(Material material)
  {
        Material[] materials = rend.materials ;
        materials[2] = material;
        rend.materials = materials ;
  }
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

167 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 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 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 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 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 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 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 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 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 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

Can I use MaterialPropertyBlock to set properties per material instead of per renderer? 0 Answers

Unable to edit grey ,,locked,, Materials 3 Answers

Slime shader (Fall Guys like material) 0 Answers

How to fix: SpeedTree Leaves and Bark using same material. 1 Answer

Black Material when camera rotate 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