【问题标题】:Collectable object doesn't work once I prefab it一旦我预制了可收集的对象,它就不起作用
【发布时间】:2016-04-24 23:55:27
【问题描述】:

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

有什么想法吗?干杯!

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);
    }

    }
}

【问题讨论】:

  • 你预制和实例化的游戏对象是什么,收藏品还是birdParticleObject?
  • 您是否使用公共变量并将它们链接到场景中的事物?不要忘记您不能“放入预制件”任何指向您的场景的链接。 (当然,预制件不知道场景 - 明天它可能会出现在完全不同的游戏中!)
  • 虽然,Prefab 不能有任何对场景对象的持久引用,但它可以拥有自己的对象。

标签: c# unity3d


【解决方案1】:

我认为您的问题在于:

birdParticlesystem.Play();
Destroy(gameObject);

您告诉粒子系统播放,然后立即销毁它的父级,这也会销毁它。试试:

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

这将在销毁可收集对象之前从其父级移除粒子系统。你应该在粒子系统播放完毕后Destroy(),否则它会留在场景中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-09
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 2020-04-03
    • 1970-01-01
    相关资源
    最近更新 更多