Grow Animation
Grow Animation

Reputation: 163

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);

        newSmoke.GetComponent<MeshRenderer>().material.SetColor("_Color",Color.red);
    }

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?

Upvotes: 0

Views: 921

Answers (1)

Hellium
Hellium

Reputation: 7346

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

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

https://docs.unity3d.com/ScriptReference/ParticleSystem-startColor.html

Upvotes: 2

Related Questions