Search Unity

Change object material

Discussion in 'Scripting' started by reddogg, Jul 22, 2013.

  1. reddogg

    reddogg

    Joined:
    Jun 17, 2013
    Posts:
    11
    Hi,
    I know this is an annoying question but I can't find an exhaustive answer on forum.
    I want do a script that on touch (mobile Device) change the material of an object.
    If it possible i should have button (or buttons) on gui that when pressed change the material of the object.
    If someone have an example or tutorial of this it wil be fantastic. :cool:

    Tank You
     
  2. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
  3. reddogg

    reddogg

    Joined:
    Jun 17, 2013
    Posts:
    11
    Hi,
    I'm trying but no result. I think it's simple but I'm new to Unity.
    I have a simple scene with one sphere, one plane and two materials, A and B.

    This is the script I've create:


    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SwitchMat : MonoBehaviour {
    5.    
    6.     var Sphere;
    7.     var A;
    8.     var B;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         Sphere = GameObject.Find("Sphere");
    13.         }
    14.        
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.        
    19.         if (GUI.Button (new Rect(0, 0, 100, 20), "RED"))
    20.             GameObject.renderer.material = A;
    21.            
    22.        
    23.         if (GUI.Button (new Rect(0, 30, 100, 20), "GREEN"))
    24.             GameObject.renderer.material = B;
    25.            
    26.  
    27.         }
    28. }
    29.  
    But I have no results.