通过脚本访问 Unity 粒子发射器



我创建了一个粒子系统,在游戏中的某个时刻,我想增加最大粒子数。

因此,我创建了一个游戏对象,并在其上附加了一个盒子 2D 碰撞体,当它与玩家接触时会触发。在该脚本中,我创建了一个公共游戏对象 leafParticleSystem,并将粒子系统拖入其中。

但是,在我的代码中,我无法做任何事情

leafParticleSystem.maxParticles=500; 

leafParticleSystem.emission.rate=5.0f

我的代码也说

"MissingComponentException: There is no 'ParticleSystem' attached to the "Herld" game object, but a script is trying to access it.
You probably need to add a ParticleSystem to the game object "Herld". Or your script needs to check if the component is attached before using it.untime/Export/Coroutines.cs:17)"

但是,这个"Herld"游戏对象不是粒子系统(我也不想在其中添加一个),但是,它确实附加了我正在拖动粒子系统(叶粒子系统)的脚本。我只是尝试从"Herld"对象中的代码修改叶粒子系统。

如果我能得到任何帮助,将不胜感激

附加脚本

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class WindEffect : MonoBehaviour {
//  Rigidbody2D _rigidbody;
    // Use this for initialization
    //Storm Warning
    public GameObject AOE;
    public GameObject UIStormInterface;
    public GameObject UIWindZone;
    public GameObject LeafStormParticleSystem;

    public float ActivateFor=5.0f;
    public float stormLength=5.0f; 

    void Start () {
    }
    // Update is called once per frame
    void Update () {
    }
    void Awake () {
//      _rigidbody = GetComponent<Rigidbody2D> ();
    }
    void OnTriggerEnter2D(Collider2D col)
    {
    // Debug.Log("Object touched trigger");
    //  Debug.Log (col.transform.name); 
        if (col.gameObject.name == "Player") {
            Debug.Log ("Collided with Storm Herald");
            StartCoroutine (TemporarilyActivateStormWarning (ActivateFor));
        }
    }
    private IEnumerator TemporarilyActivateStormWarning(float ActivateFor) {
    //Show Storm Warning
        this.gameObject.GetComponent<BoxCollider2D>().enabled=(false);
        UIStormInterface.GetComponent<Text>().text="A Violent Storm is Approaching";
        UIStormInterface.SetActive(true);
        UIWindZone.SetActive (true);
        //Wait for 5 seconds then activate storm for 5 seconds
        yield return new WaitForSeconds(ActivateFor);
        UIStormInterface.SetActive(false);
        AOE.SetActive (true);
        //  ParticleSystem LeafStormParticleSystem = GetComponent<ParticleSystem>();
    //  LeafStormParticleSystem.emission.rate = 5.0f;
        LeafStormParticleSystem.maxParticles = 500;

        yield return new WaitForSeconds(stormLength);

        //Turn off Storm
        AOE.SetActive (false);
        //Tell user storm is stopped for 2 seconds.
        UIStormInterface.GetComponent<Text>().text="Storm has Passed";
        UIStormInterface.SetActive(true);
        yield return new WaitForSeconds(3);
        UIStormInterface.SetActive(false);
        UIWindZone.SetActive (false);
    }
    void OnTriggerStay2D(Collider2D col)
    {
        if (col.gameObject.name == "WindObject") {
            Debug.Log ("Collided");
            //  Debug.Log (col.transform.name); 
        //  Wind();
            //StartCoroutine(WindStart());
        }
    }
    /*
    IEnumerator WindStart(){
        Debug.LogError ("Slow Time.");
        //SlowTheTime();
    //  yield return new WaitForSeconds(3.5f);
        //  Debug.LogError ("Slow Time.");
    //  Wind ();
    }*/

    void OnCollisionEnter2D(Collision2D col)
    {
//      Debug.Log (col.transform.name); 
    }
    void OnTriggerStay(Collider col)
    {
        Debug.Log("Object is in trigger");
    }
    void OnTriggerExit(Collider other)
    {
        Debug.Log("Object left the trigger");
    }

    /*
    void Wind(){
        _rigidbody = GetComponent<Rigidbody2D> ();
        Debug.Log ("test got here");
        Debug.Log (this.gameObject.name);
        this._rigidbody.AddForce (-Vector2.left * 100 );
    }*/
}

目前,LeafStormParticleSystem变量为您提供对ParticleSystem组件所在的游戏对象的引用,而不是ParticleSystem本身。因此,您无法访问粒子系统的成员,如 maxParticlesemission 。您可以采取两种方法来更正此问题:

当您需要访问粒子系统时,请调用GetComponent<ParticleSystem>()如果过于频繁地执行此操作,这可能会很昂贵 - 修改成员的语法将是:

LeafStormParticleSystem.GetComponent<ParticleSystem>().maxParticles = 500;

LeafStormParticleSystemGameObject更改为ParticleSystem这允许您在编辑器中分配对粒子系统的直接引用LeafStormParticleSystem,并且比重复调用GetComponent()更有效。语法为:

// Change the datatype
public ParticleSystem LeafStormParticleSystem;
// [...]
// To modify members later, you can do as you're currently doing:
LeafStormParticleSystem.maxParticles = 500;
// Modifying the emission rate is a bit less straightforward, because emission is a struct
var newEmission = LeafStormParticleSystem.emission;
newEmission.rate = new ParticleSystem.MinMaxCurve(5.0f);
LeafStormParticleSystem.emission = newEmission;

希望这有帮助!如果您有任何问题,请告诉我。

编辑:现在我看到了你的脚本和...是的。。您没有对粒子系统的引用

在这里,您不访问粒子系统对象,而是访问包含它的游戏对象:LeafStormParticleSystem.maxParticle = 500;

引用粒子系统本身,而不是游戏对象。

LeafStormParticleSystem.GetComponent<ParticleSystem>().maxParticles = 500;

老:您应该获得对粒子系统的引用(在我的情况下,我停止它),例如在启动函数中:

private ParticleSystem ps;
void Start()
{
    ps = GetComponentInChildren<ParticleSystem>();
    ps.Stop();
}

之后,您可以启动或编辑粒子(在我的情况下,在单独的函数中播放和更改maxParticles):

void StartEmission()
{
    ps.Play();
    ps.maxParticles = 500;
}

解决方案 1:

只需尝试将LeafStormParticleSystem的数据类型更改为ParticleSystem即可。这样您就可以在LeafStormParticleSystem变量的帮助下访问与粒子系统相关的方法和属性

public ParticleSystem LeafStormParticleSystem;

解决方案 2:

您无法直接访问游戏对象的粒子系统方法和属性,除非您未将该变量类型转换为粒子系统类型。

为此,您可以尝试:

LeafStormParticleSystem.GetComponent<ParticleSystem>().maxParticles = 500;

((ParticleSystem)LeafStormParticleSystem).maxParticles = 500;

最新更新