简体   繁体   English

从脚本更改预制颜色

[英]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): 如您在图片中所看到的,预制件是一种材料,但脚本根本没有改变颜色(图片中的黑色): 在此处输入图片说明

Do you have any idea on how to solve this? 您对如何解决这个问题有任何想法吗?

You must change the start color of the ParticleSystem , not the material of an nonexistent MeshRenderer . 您必须更改“ ParticleSystem的开始颜色,而不是不存在的MeshRenderer

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM