Grow Animation Grow Animation - 1 year ago 99
C# Question

Change prefab color from script

I have a smoke prefab that I need to change the color from script since the color will be change during playtime.

I am instantiating the prefab and attempting to change color to red like this:

class Smoke1 : MonoBehaviour
public GameObject myPrefab;
public GameObject canvasObject;

void Start()
GameObject newSmoke = Instantiate(myPrefab, new Vector3(397, -394, 90), Quaternion.Euler(-90, 0, 0)) as GameObject;
newSmoke.transform.SetParent(canvasObject.transform, false);
newSmoke.transform.localScale = new Vector3(1, 1, 1);


As you can see in the picture, the prefab as a material but the script is not changing the color at all (black in the picture):
enter image description here

Do you have any idea on how to solve this?

Answer Source

You must change the start color of the ParticleSystem, not the material of an nonexistent MeshRenderer.

   newSmoke.GetComponent<ParticleSystem>().startColor = ;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download