Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Over my head with Material Switching

Discussion in 'Scripting' started by ChaosRobin, Mar 28, 2015.

  1. ChaosRobin

    ChaosRobin

    Joined:
    Mar 28, 2013
    Posts:
    58
    Hey all.

    So, I got into a task which I thought wouldn't be too difficult, but it has been kicking my but. What I am trying to do, is instantiate an object, have it come in with one material (A reverse dissolve invisible to visible) then switch to its 'normal' material, then when another object is instantiated, I want this one to run its dissolve material again, this time, visible to invisible then destroy the object. The problem is.. IVE GOT IT ALL WORKING, in like 3 different ways. At first I didnt instantiate, i had all of the objects as a child of the object calling them just switching from active to inactive, but my problem with this was, once the material had faded out, bring the object back in didnt reset the material, so i figured for that reason and others that I would run it as an instantiate, but now my code isnt working on the shader. I think its because the material pops up as an 'instance' and the code is trying to apply the changes to the original material but not a instance of that material. So I need to know how to tell my code to apply its changes to the instantiated material. if that even makes sense. To try to clear things up a bit ive included 5! scripts, the 5 im using to make this 'work' maybe you can offer me some guidance and help me fix this thing! Sorry about all of the commented out lines, i try things, then change them, but keep the lines i might need.



    Code (csharp):
    1.  
    2.     using UnityEngine;
    3.     using System.Collections;
    4.     using System.Collections.Generic;
    5.        
    6.     public class MultipulCharacterSwitcher1 : MonoBehaviour {
    7.            
    8.            
    9.         public GameObject Character1 ;
    10.         public GameObject Character2 ;
    11.    
    12.    
    13.         public GameObject controller;
    14.         public GameObject currentCharacter;
    15.         public GameObject previousCharacter;
    16.         public MaterialSwap currentCharacterScripts;
    17.         public MaterialSwap1 previousCharacterScripts;
    18.    
    19.         public MaterialSwap Character1In;
    20.         public MaterialSwap1 Character1Out;
    21.         public MaterialSwap Character2In;
    22.         public MaterialSwap1 Character2Out;
    23.    
    24.         public bool character1Active = false;
    25.         public bool character2Active = false;
    26.    
    27.         public GameObject Effects;
    28.         public float effectsTime;
    29.    
    30.    
    31.         void Awake(){
    32.             currentCharacter = Character1;
    33.             currentCharacterScripts = Character1In;
    34.             GameObject childObject = Instantiate (Character1, controller.transform.position, controller.transform.rotation) as GameObject;
    35.             childObject.transform.parent = gameObject.transform;
    36.             character1Active = true;
    37.         }
    38.    
    39.         void  Start (){
    40.             Effects.gameObject.active  = false;
    41.             //Character1.GetComponentInChildren<MaterialSwap>().enabled = true;
    42.             //Character1.GetComponentInChildren<Timed>().enabled = true;
    43.             currentCharacterScripts.enabled = true;
    44.             previousCharacterScripts.enabled = true;
    45.         }
    46.    
    47.         void  Update (){
    48.    
    49.                
    50.             if (Input.GetKeyDown(KeyCode.F1)){
    51.    
    52.                 previousCharacter = currentCharacter;
    53.                 //previousCharacterScripts = currentCharacterScripts;
    54.                 StartCoroutine(EffectsActive());
    55.                 Spawn2();
    56.    
    57.                 }
    58.         }
    59.         void Spawn2 (){
    60.    
    61.             //previousCharacterScripts.enabled = true;
    62.             //currentCharacterScripts.enabled = true;
    63.             GameObject childObject = Instantiate (Character2, controller.transform.position, controller.transform.rotation) as GameObject;
    64.             childObject.transform.parent = gameObject.transform;
    65.             //Character2.GetComponentInChildren<MaterialSwap>().enabled = true;
    66.             //Character2.GetComponentInChildren<Timed>().enabled = true;
    67.             currentCharacter = Character1;
    68.            
    69.         }
    70.            
    71.         //
    72.         IEnumerator EffectsActive(){
    73.             print ("Effects Called");
    74.             Effects.gameObject.active = true;
    75.             yield return new WaitForSeconds (effectsTime);
    76.             Effects.gameObject.active = false;
    77.         }
    78.    
    79.     }
    80.  


    Code (csharp):
    1.  
    2.  
    3.     using UnityEngine;
    4.     using System.Collections;
    5.    
    6.     public class Timed : MonoBehaviour
    7.     {
    8.         public float m_fDestruktionSpeed = 0.1f;
    9.         public Material m_Mat;
    10.         public float m_fTime;
    11.        
    12.         public float resetTime;
    13.        
    14.         void Awake(){
    15.             m_Mat.SetFloat ("_Amount", resetTime);  
    16.         }
    17.        
    18.         void OnEnable(){
    19.             resetTime = m_fTime;
    20.         }
    21.        
    22.         void Start () {
    23.             m_fTime = resetTime;
    24.             m_Mat = GetComponent<Renderer>().material;
    25.         }
    26.        
    27.         void Update () {
    28.             m_fTime += Time.deltaTime * m_fDestruktionSpeed;
    29.             if (m_fTime <= 0.0f)
    30.                 m_fTime = 0.0f;
    31.             //this.GetComponent<Timed>().enabled = false;
    32.             m_Mat.SetFloat("_Amount", m_fTime);
    33.         }
    34.     }
    35.  



    Code (csharp):
    1.  
    2.  
    3.     using UnityEngine;
    4.     using System.Collections;
    5.    
    6.    
    7.     public class MaterialSwap : MonoBehaviour {
    8.    
    9.         public GameObject me;
    10.         public GameObject notMe1;
    11.         public GameObject notMe2;
    12.         public GameObject notMe3;
    13.         public GameObject notMe4;
    14.         public KeyCode switchKey;
    15.         public Material myMaterial;
    16.         public Material myOtherMaterial;
    17.         public Renderer rend;
    18.         public float effectTime;
    19.    
    20.    
    21.         // Use this for initialization
    22.         void Start () {
    23.             rend = GetComponent<Renderer>();
    24.             rend.enabled = true;
    25.             rend.sharedMaterial = myOtherMaterial;
    26.             this.gameObject.GetComponent<MaterialSwap>().enabled = true;
    27.             this.gameObject.GetComponent<MaterialSwap1>().enabled = false;
    28.             this.gameObject.GetComponent<Timed>().enabled = true;
    29.             this.gameObject.GetComponent<Timed1>().enabled = false;
    30.             //notMe1.gameObject.GetComponentInChildren<MaterialSwap1>().enabled = true;
    31.             //notMe2.gameObject.GetComponentInChildren<MaterialSwap1>().enabled = true;
    32.             //notMe3.gameObject.GetComponentInChildren<MaterialSwap1>().enabled = true;
    33.             //notMe4.gameObject.GetComponentInChildren<MaterialSwap1>().enabled = true;
    34.             StartCoroutine(EffectWait());
    35.         }
    36.        
    37.         // Update is called once per frame
    38.         void FixedUpdate () {
    39.             if (Input.GetKey (switchKey)) {
    40.                 //this.GetComponent<MaterialSwap1>().enabled = true;
    41.             }
    42.    
    43.         }
    44.    
    45.    
    46.         IEnumerator EffectWait(){
    47.             print(Time.time);
    48.             yield return new WaitForSeconds (effectTime);
    49.             rend.sharedMaterial = myMaterial;
    50.             this.GetComponent<Timed>().enabled = false;
    51.             //this.GetComponent<MaterialSwap1>().enabled = true;
    52.             this.GetComponent<MaterialSwap>().enabled = false;
    53.             print(Time.time);
    54.         }
    55.    
    56.     }
    57.  


    Code (csharp):
    1.  
    2.     using UnityEngine;
    3.     using System.Collections;
    4.    
    5.     public class Timed1 : MonoBehaviour
    6.     {
    7.         public float m_fDestruktionSpeed = 0.1f;
    8.         public Material m_Mat;
    9.         public float m_fTime;
    10.    
    11.         public float resetTime;
    12.    
    13.         void Awake(){
    14.             m_Mat.SetFloat ("_Amount", resetTime);  
    15.         }
    16.    
    17.         void OnEnable(){
    18.             resetTime = m_fTime;
    19.         }
    20.        
    21.         void Start () {
    22.             m_fTime = resetTime;
    23.             m_Mat = GetComponent<Renderer>().material;
    24.         }
    25.        
    26.         void Update () {
    27.             m_fTime += Time.deltaTime * m_fDestruktionSpeed;
    28.             if (m_fTime >= 0.5f)
    29.                 m_fTime = 0.5f;
    30.             //this.GetComponent<Timed>().enabled = false;
    31.             m_Mat.SetFloat("_Amount", m_fTime);
    32.         }
    33.     }
    34.  

    Code (csharp):
    1.  
    2.     using UnityEngine;
    3.     using System.Collections;
    4.    
    5.    
    6.     public class MaterialSwap1 : MonoBehaviour {
    7.    
    8.         public GameObject myParrent;
    9.         public Material myMaterial;
    10.         public Material myOtherMaterial;
    11.         public Renderer rend;
    12.         public float effectTime;
    13.        
    14.        
    15.         // Use this for initialization
    16.         void Start () {
    17.             rend = GetComponent<Renderer>();
    18.             rend.enabled = true;
    19.             rend.sharedMaterial = myOtherMaterial;
    20.             this.GetComponent<Timed1>().enabled = true;
    21.             StartCoroutine(EffectWait());
    22.         }
    23.        
    24.         // Update is called once per frame
    25.         void Update () {
    26.    
    27.         }
    28.        
    29.        
    30.         IEnumerator EffectWait(){
    31.             print(Time.time);
    32.             this.GetComponentInParent<MeshResetter>().enabled = true;
    33.             yield return new WaitForSeconds (effectTime);
    34.             this.GetComponent<Timed1>().enabled = false;
    35.             StartCoroutine(EffectWait1());
    36.             print(Time.time);
    37.         }
    38.         IEnumerator EffectWait1(){
    39.             yield return new WaitForSeconds (effectTime);
    40.         }
    41.            
    42.     }
    43.