【发布时间】: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 不能有任何对场景对象的持久引用,但它可以拥有自己的对象。