Search Unity

Setting multiple materials from script

Discussion in 'Scripting' started by wayne1512, Feb 27, 2018.

  1. wayne1512

    wayne1512

    Joined:
    Oct 23, 2017
    Posts:
    84
    So I have a mesh with 2 materials and I need it to change material every so often. Basically I need to do what is explained it this pic.

    I wrote this code but i have some problems:

    1. Even thou I made a variable for the renderer called rend rend.Material isn't working and I need to do the full getComponent
    2. This is changing the 1st material. What do I do to change the 2nd material???

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Pin : MonoBehaviour {
    6.  
    7.     public int timer;
    8.     public int CurrentMaterial = 0;
    9.  
    10.     public Material[] Materials;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.  
    15.         Renderer rend = GetComponent<Renderer>();
    16.  
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.  
    22.         timer++;
    23.  
    24.         if (timer >= 100) {
    25.             timer = 0;
    26.  
    27.             if (CurrentMaterial == Materials.Length) {
    28.                 CurrentMaterial = 0;
    29.             }else{
    30.                 CurrentMaterial++;
    31.             }
    32.  
    33.             GetComponent<Renderer>().material = Materials[CurrentMaterial];
    34.  
    35.         }
    36.  
    37.  
    38.     }
    39. }
    40.  
     
  2. Verdemis

    Verdemis

    Joined:
    Apr 27, 2013
    Posts:
    92
    Not tested, but I think this should work.

    Get the array of assigned materials from the renderer, change it, and assign the modified materials array back to the renderer. To get all materials you need to use GetComponent<Renderer>().materials, this will return the array with all the used materials. If you use GetComponent<Renderer>().material (without the s), you will only get the first material.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Pin : MonoBehaviour {
    5.     public int timer;
    6.     public int CurrentMaterial = 0;
    7.     public Material[] Materials;
    8.     // Use this for initialization
    9.     void Start () {
    10.         Renderer rend = GetComponent<Renderer>();
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update () {
    15.         timer++;
    16.         if (timer >= 100) {
    17.             timer = 0;
    18.             if (CurrentMaterial == Materials.Length) {
    19.                 CurrentMaterial = 0;
    20.             }else{
    21.                 CurrentMaterial++;
    22.             }
    23.  
    24.             Material[] currentlyAssignedMaterials = GetComponent<Renderer>().materiasl;
    25.  
    26.             currentlyAssignedMaterials[theIndexYouWantToChange] = Materials[CurrentMaterial];
    27.             GetComponent<Renderer>().materials = currentlyAssignedMaterials;
    28.         }
    29.     }
    30. }
     
  3. wayne1512

    wayne1512

    Joined:
    Oct 23, 2017
    Posts:
    84
    Im going to test it right now.

    But Instead of GetComponent<Renderer>().material can't you do rend.material .... In theory that should work but it's not working.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Yes, but you wrote yours incorrectly. You must moved the declaration outside of Start()
    Code (csharp):
    1. Renderer rend;
    2. void Start() {
    3.    rend = GetComponent<Renderer>();
    4.   }
    5. // now it's valid.
     
  5. wayne1512

    wayne1512

    Joined:
    Oct 23, 2017
    Posts:
    84
    Tnx Methos

    It's still giving me THESE errors.

    Edit: Ok fixed it:
    turns out it was .materials not .material
    Must have copied wrong