- Home /
Material Switching Upon Instantiation. over my head
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.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MultipulCharacterSwitcher1 : MonoBehaviour {
public GameObject Character1 ;
public GameObject Character2 ;
public GameObject controller;
public GameObject currentCharacter;
public GameObject previousCharacter;
public MaterialSwap currentCharacterScripts;
public MaterialSwap1 previousCharacterScripts;
public MaterialSwap Character1In;
public MaterialSwap1 Character1Out;
public MaterialSwap Character2In;
public MaterialSwap1 Character2Out;
public bool character1Active = false;
public bool character2Active = false;
public GameObject Effects;
public float effectsTime;
void Awake(){
currentCharacter = Character1;
currentCharacterScripts = Character1In;
GameObject childObject = Instantiate (Character1, controller.transform.position, controller.transform.rotation) as GameObject;
childObject.transform.parent = gameObject.transform;
character1Active = true;
}
void Start (){
Effects.gameObject.active = false;
//Character1.GetComponentInChildren<MaterialSwap>().enabled = true;
//Character1.GetComponentInChildren<Timed>().enabled = true;
currentCharacterScripts.enabled = true;
previousCharacterScripts.enabled = true;
}
void Update (){
if (Input.GetKeyDown(KeyCode.F1)){
previousCharacter = currentCharacter;
//previousCharacterScripts = currentCharacterScripts;
StartCoroutine(EffectsActive());
Spawn2();
}
}
void Spawn2 (){
//previousCharacterScripts.enabled = true;
//currentCharacterScripts.enabled = true;
GameObject childObject = Instantiate (Character2, controller.transform.position, controller.transform.rotation) as GameObject;
childObject.transform.parent = gameObject.transform;
//Character2.GetComponentInChildren<MaterialSwap>().enabled = true;
//Character2.GetComponentInChildren<Timed>().enabled = true;
currentCharacter = Character1;
}
//
IEnumerator EffectsActive(){
print ("Effects Called");
Effects.gameObject.active = true;
yield return new WaitForSeconds (effectsTime);
Effects.gameObject.active = false;
}
}
using UnityEngine;
using System.Collections;
public class Timed : MonoBehaviour
{
public float m_fDestruktionSpeed = 0.1f;
public Material m_Mat;
public float m_fTime;
public float resetTime;
void Awake(){
m_Mat.SetFloat ("_Amount", resetTime);
}
void OnEnable(){
resetTime = m_fTime;
}
void Start () {
m_fTime = resetTime;
m_Mat = GetComponent<Renderer>().material;
}
void Update () {
m_fTime += Time.deltaTime * m_fDestruktionSpeed;
if (m_fTime <= 0.0f)
m_fTime = 0.0f;
//this.GetComponent<Timed>().enabled = false;
m_Mat.SetFloat("_Amount", m_fTime);
}
}
using UnityEngine;
using System.Collections;
public class MaterialSwap : MonoBehaviour {
public GameObject me;
public GameObject notMe1;
public GameObject notMe2;
public GameObject notMe3;
public GameObject notMe4;
public KeyCode switchKey;
public Material myMaterial;
public Material myOtherMaterial;
public Renderer rend;
public float effectTime;
// Use this for initialization
void Start () {
rend = GetComponent<Renderer>();
rend.enabled = true;
rend.sharedMaterial = myOtherMaterial;
this.gameObject.GetComponent<MaterialSwap>().enabled = true;
this.gameObject.GetComponent<MaterialSwap1>().enabled = false;
this.gameObject.GetComponent<Timed>().enabled = true;
this.gameObject.GetComponent<Timed1>().enabled = false;
//notMe1.gameObject.GetComponentInChildren<MaterialSwap1>().enabled = true;
//notMe2.gameObject.GetComponentInChildren<MaterialSwap1>().enabled = true;
//notMe3.gameObject.GetComponentInChildren<MaterialSwap1>().enabled = true;
//notMe4.gameObject.GetComponentInChildren<MaterialSwap1>().enabled = true;
StartCoroutine(EffectWait());
}
// Update is called once per frame
void FixedUpdate () {
if (Input.GetKey (switchKey)) {
//this.GetComponent<MaterialSwap1>().enabled = true;
}
}
IEnumerator EffectWait(){
print(Time.time);
yield return new WaitForSeconds (effectTime);
rend.sharedMaterial = myMaterial;
this.GetComponent<Timed>().enabled = false;
//this.GetComponent<MaterialSwap1>().enabled = true;
this.GetComponent<MaterialSwap>().enabled = false;
print(Time.time);
}
}
using UnityEngine;
using System.Collections;
public class Timed1 : MonoBehaviour
{
public float m_fDestruktionSpeed = 0.1f;
public Material m_Mat;
public float m_fTime;
public float resetTime;
void Awake(){
m_Mat.SetFloat ("_Amount", resetTime);
}
void OnEnable(){
resetTime = m_fTime;
}
void Start () {
m_fTime = resetTime;
m_Mat = GetComponent<Renderer>().material;
}
void Update () {
m_fTime += Time.deltaTime * m_fDestruktionSpeed;
if (m_fTime >= 0.5f)
m_fTime = 0.5f;
//this.GetComponent<Timed>().enabled = false;
m_Mat.SetFloat("_Amount", m_fTime);
}
}
using UnityEngine;
using System.Collections;
public class MaterialSwap1 : MonoBehaviour {
public GameObject myParrent;
public Material myMaterial;
public Material myOtherMaterial;
public Renderer rend;
public float effectTime;
// Use this for initialization
void Start () {
rend = GetComponent<Renderer>();
rend.enabled = true;
rend.sharedMaterial = myOtherMaterial;
this.GetComponent<Timed1>().enabled = true;
StartCoroutine(EffectWait());
}
// Update is called once per frame
void Update () {
}
IEnumerator EffectWait(){
print(Time.time);
this.GetComponentInParent<MeshResetter>().enabled = true;
yield return new WaitForSeconds (effectTime);
this.GetComponent<Timed1>().enabled = false;
StartCoroutine(EffectWait1());
print(Time.time);
}
IEnumerator EffectWait1(){
yield return new WaitForSeconds (effectTime);
}
}