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

时间:2017-01-13 18:58:50

标签: c# unity3d

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

结果,我创建了一个游戏对象并附加了一个盒子2d对撞机,当它与玩家接触时会发射。在该脚本中,我创建了一个公共GameObject 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" GameObject不是粒子系统(也不想添加一个)但它确实附加了我将粒子系统(叶子粒子系统)拖动到的脚本。我只是试图修改我的代码中的叶子粒子系统" 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 );
    }*/

}

3 个答案:

答案 0 :(得分:0)

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

在这里你不访问ParticleSystem对象,你访问包含它的GameObject: LeafStormParticleSystem.maxParticles = 500;

引用ParticleSystem本身,而不是GameObject。

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

OLD: 你应该得到你的粒子系统的参考(在我的例子中我停止它),例如在Start函数中:

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

之后你可以开始或编辑粒子(在我的情况下,在一个单独的函数中进行游戏并更改maxParticles):

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

答案 1 :(得分:0)

目前,您的LeafStormParticleSystem变量会为您提供ParticleSystem组件所在的GameObject的引用,而不是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;

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

答案 2 :(得分:0)

解决方案1:

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

public ParticleSystem LeafStormParticleSystem;

解决方案2:

您不能直接访问GameObject的ParticleSystem方法和属性,除非并且直到您没有将该变量类型转换为ParticleSystem类型。

为此,您可以尝试:

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

((ParticleSystem)LeafStormParticleSystem).maxParticles = 500;