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 Aeleck · May 06, 2015 at 12:33 PM · instantiatematerialcolor

Instantiating objects with their own materials

Hello, I am currently working on a firework system. I'm stuck at a part where I would like to create a new material for each "flare" in the firework (which is a prefab being instantiated), and I have tried several different methods, none of which seem to work. I have come to understand that using gameObject.GetComponent().material will create a new instance of the sharedMaterial, however, I can't seem to do this without unity freaking out with the error of "Instantiating material due to calling renderer.material during edit mode. This will leak materials into the scene. You most likely want to use renderer.sharedMaterial instead." But I do want each of my prefabs to have their own material so I can adjust them individually as the firework is being played. Here's the firework class:

 using UnityEngine;
 using UnityEditor;
 using UnityEngine.Serialization;
 using System.Collections;
 using System.Collections.Generic;
 
 [ExecuteInEditMode]
 public class Firework : MonoBehaviour
 {
     public int
         numberOfFlares = 50;
     public float flareSize = 1.0f;
     public GameObject flarePrefab;
     public Color
         colorOnRising;
     public bool
         gravityOnRising = false;
     public Vector3 startingVelocity = new Vector3 (0, 1000, 0);
     public float
         explodeTime = 100f;
     public float explodeForce = 50f;
     public bool gravityOnExplode = true;
     public float flareLifeTime = 5f;
 
     private float timePassed = 0.0f;
     private float timeSinceLaunch = 0.0f;
     private float timeSinceExplostion = 0.0f;
 
     private List<GameObject> flares = new List<GameObject> ();
 
     public enum Phase
     {
         NotLaunched,
         Rising,
         Exploded
     }
 
     public Phase
         currentPhase = Phase.NotLaunched;
 
     public void Launch ()
     {
         if (currentPhase == Phase.NotLaunched) {
             //ready to launch!
             Debug.Log ("Launched!");
             currentPhase = Phase.Rising;
             for (int amount = 0; amount < numberOfFlares; amount++) {
                 GameObject go = (GameObject)Instantiate (flarePrefab, gameObject.transform.position, gameObject.transform.rotation);
                 flares.Add (go);
             }
             foreach (GameObject go in flares) {
                 go.transform.localScale = new Vector3 (flareSize, flareSize, flareSize);
                 go.GetComponent<MeshRenderer> ().material.SetColor ("_Emission", colorOnRising);
                 go.GetComponent<Rigidbody> ().useGravity = gravityOnRising;
                 go.GetComponent<Rigidbody> ().velocity = startingVelocity;
             }
         }
     }
 
     public void Refresh ()
     {
         Debug.Log ("Refreshed!");
 
         bool launchAfterRefresh = (currentPhase != Phase.NotLaunched);
 
         timePassed = 0;
         currentPhase = Phase.NotLaunched;
         foreach (GameObject go in flares) {
             DestroyImmediate (go);
         }
         flares.Clear ();
 
         if (launchAfterRefresh) {
             Launch ();
         }
 
     }
 
     public void Stop ()
     {
         Debug.Log ("Stopped");
 
         currentPhase = Phase.NotLaunched;
         Refresh ();
     }
 
     // Use this for initialization
     void Start ()
     {
         Debug.Log ("Started!");
 
         Refresh ();
         Launch ();
 
     }
     
     // Update is called once per frame
     public void Update ()
     {
         foreach (GameObject go in flares) {
             go.transform.LookAt (Camera.main.transform.position);
         }
         if (currentPhase != Phase.NotLaunched) {
             timePassed += Time.deltaTime;
         }
     }
 }






Comment
Add comment · Show 1
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
avatar image JinJin · May 08, 2015 at 12:00 PM 0
Share

create a pool - a "global" list of materials (there is no global in C# but you can have a static class or one game controller class) and use this list to store materials to reuse them. you can Instantiate() and Destroy() materials as you like.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Changing two different objects renderer colour 1 Answer

Random.Range doesn't work 1 Answer

Combination of Instance and random in a network game 2 Answers

Instantiate object with Emissive Material 2 Answers


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