Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

material change

Discussion in 'Scripting' started by Truls_, Jun 11, 2018.

  1. Truls_

    Truls_

    Joined:
    Dec 4, 2016
    Posts:
    103
    I have this house model where if I click on a wall, the wall changes its material, but if I click on another wall I want the first wall to change back to its original material and the second wall to change to a new material. How would I do this?
     
  2. bakir-omarov

    bakir-omarov

    Joined:
    Aug 1, 2015
    Posts:
    48
    Hey, you can use a bunch of methods for it. Simplest way it to use Ray to detecting what you clicked with mouse, and then change it material, but before change, you have to store it previous material and the link to current wall. I commented everything for you ;)

    Just don't forget to tag every wall with Wall tag. And drag&drop your new material to script!

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class UnityAnswers_MaterialChanger : MonoBehaviour
    7. {
    8.  
    9.     public Material newMaterial; // drag here material from project folder in Unity Editor
    10.     private GameObject m_previousWall; // we will hold here our wall's gameobject , so next time when we gonna change another wall, we can find previous wall
    11.     private Material m_previousMaterial; // we will hold material of our wall that was before we changed it
    12.  
    13.     void Update()
    14.     {
    15.         if (Input.GetMouseButtonDown(0))
    16.         {
    17.             // ray using our mouse position, so we can click and hit any object on the screen
    18.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    19.             RaycastHit hit;
    20.  
    21.             if (Physics.Raycast(ray, out hit))
    22.             {
    23.                 if (hit.transform.tag == "Wall") // add Wall tag to your walls
    24.                 {
    25.                     if(m_previousWall != null)
    26.                     {
    27.                         // if we has a m_previousWall, then we have to change it material to previous material that it had
    28.                         m_previousWall.GetComponent<Renderer>().material = m_previousMaterial;
    29.                     }
    30.                     // save our current wall as a previous
    31.                     m_previousWall = hit.collider.gameObject;
    32.                     // save our current material as a previous material, so we can attach it to previous wall when we need it
    33.                     m_previousMaterial = hit.collider.gameObject.GetComponent<Renderer>().material;
    34.                     // change current wall material to new material that we want
    35.                     hit.collider.gameObject.GetComponent<Renderer>().material = newMaterial;
    36.                     Debug.Log("This is a Wall, and i changed its material");
    37.                 }
    38.                 else
    39.                 {
    40.                     // if we hitted some collider, but it wasn't a wall (because it has another tag)
    41.                     Debug.Log("This isn't a Wall, this is " + hit.collider.gameObject.name);
    42.                 }
    43.             }
    44.         }
    45.     }
    46. }
    47.  
    48.