首页 文章

一旦我预制它,可收集的物体就不起作用了

提问于
浏览
1

我有一个可收集的对象,它有另一个对象作为孩子,附加了一个粒子系统 . 当玩家遇到可收集对象时,应该销毁收集器并且应该播放粒子系统 . 当游戏对象放置在场景中时,下面的代码正常工作,但是一旦我预装它并在代码中实例化它 - 粒子系统就不再有效了 .

有任何想法吗?干杯!

using UnityEngine;
using System.Collections;

public class collectable : MonoBehaviour {

    GameObject birdParticleObject; 
    ParticleSystem birdParticlesystem; 


    void Start () {

        birdParticleObject = GameObject.Find("BirdParticleSystem");
        birdParticlesystem = birdParticleObject.GetComponent<ParticleSystem>();
    }

    // Update is called once per frame
    void Update () {

    }


    void OnTriggerEnter(Collider other)
    {
        if(other.tag == "Player") {

            birdParticlesystem.Play();
            Destroy(gameObject);
    }

    }
}

1 回答

  • 3

    我认为你的问题在于:

    birdParticlesystem.Play();
    Destroy(gameObject);
    

    你告诉粒子系统玩,然后立即摧毁它的父母,它也会摧毁它 . 尝试:

    birdParticlesystem.Play();
         birdParticlesystem.transform.SetParent(null);
         Destroy(gameObject);
    

    这将在销毁可收集对象之前从其父级中删除粒子系统 . 一旦完成播放,你应该使用粒子系统 Destroy() ,否则它将留在场景中 .

相关问题