2016-04-23 99 views
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); 
    } 

    } 
} 
+0

你预先制作并实例化的GameObject,collectable或birdParticleObject? –

+0

你使用公共变量并将它们链接到场景中的东西?不要忘记,你不能“预设”任何链接到你的场景。 (当然,预制并不知道一个场景 - 明天它可能完全在另一场比赛中!) – Fattie

+0

尽管Prefab不能对场景对象进行任何持久引用,但它可以拥有自己的对象。 –

回答

3

我觉得你的问题在于说,该地区:

birdParticlesystem.Play(); 
Destroy(gameObject); 

你马上告诉粒子系统打,然后摧毁它的父也将破坏它。请尝试:

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

这会在销毁可收集对象之前从其父项中删除粒子系统。粒子系统一旦完成播放,你应该接着执行它,否则它将被留在场景中。

相关问题