Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ChaosRobin · Mar 28, 2015 at 04:53 PM · c#instantiatematerialrendererswap

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);
     }
         
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Changing two different objects renderer colour 1 Answer

Multiple Cars not working 1 Answer

Changing sort order of particles on instantiate? (C#) 0 Answers

Renderer Removed When Scene Started 0 Answers

How to change a value in custom shader through script, C# 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges