- Home /
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;
}
}
}
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.