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

Make Gameobject Semi-Transparent w/ Code

Discussion in 'Scripting' started by ExbowFTW, Nov 3, 2015.

  1. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Hello Everyone,

    In my game there is a powerup that allows the player to go through walls. When they get this powerup, I want the walls to become semi-transparent. I know you can do this by changing a GameObject's material's shader to Transparent/Diffuse, but how would you do this with code, while still keeping the same texture of the object?

    Thanks!
     
  2. flonch

    flonch

    Joined:
    Aug 20, 2014
    Posts:
    63
    Code (CSharp):
    1. renderer.material.shader=Shader.Find("Transparent/Diffuse");
     
  3. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Under my "Renderer" component, in the Materials section, there is no shader option. So wouldn't this code not work? All it has under it is "grid pattern 02" (my material). Grid pattern 02 is also the component-material under the object.
     
  4. EdgE790

    EdgE790

    Joined:
    Jul 29, 2015
    Posts:
    3
    You can create 2 materials: 1st will be standart, and 2nd will be semi-transparent. After getting powerup you should just replace materials.
     
  5. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    That makes sense - how do you replace materials?
     
  6. EdgE790

    EdgE790

    Joined:
    Jul 29, 2015
    Posts:
    3
    First you should create 2 materials. First is common wall material:
    Second is transparent wall material: all same, but rendering mode is Fade and alpha channel is lower:
    After this you should just place script for changing this materials.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MaterialReplace : MonoBehaviour {
    5.     public Material transparentMaterial;
    6.     bool isTransparent = false;
    7.  
    8.     float lastTime;
    9.     Material previousMaterial;
    10.  
    11.     void Start () {
    12.         lastTime = Time.time;
    13.         previousMaterial = gameObject.GetComponent<Renderer>().material;
    14.     }
    15.  
    16.     void Update(){
    17.         if (Time.time - lastTime > 3) {
    18.             lastTime = Time.time;
    19.             if (!isTransparent) {
    20.                 gameObject.GetComponent<Renderer>().material = transparentMaterial;
    21.                 isTransparent = true;
    22.             } else {
    23.                 gameObject.GetComponent<Renderer>().material = previousMaterial;
    24.                 isTransparent = false;
    25.             }
    26.         }
    27.     }
    28. }
    29.  
    Done! Now materials will replace every 3 secounds!
    Good luck in your project!
     
  7. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Wow thanks! Thanks for the pictures as well :)
     
  8. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    It worked! Thanks! :) :D