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);
}
}
}
你預先製作並實例化的GameObject,collectable或birdParticleObject? –
你使用公共變量並將它們鏈接到場景中的東西?不要忘記,你不能「預設」任何鏈接到你的場景。 (當然,預製並不知道一個場景 - 明天它可能完全在另一場比賽中!) – Fattie
儘管Prefab不能對場景對象進行任何持久引用,但它可以擁有自己的對象。 –