[英]How can I destroy a game object but have a particle effect after in unity3D?
[英]How to collect coins in Unity 3D which appears randomly in the scene, destroy the Game Object after have been collected and show the score in canvas?
我正在嘗試讓我的游戲角色收集隨機出現在場景中的硬幣,通過五個通道,並且獲得的每個硬幣的分數出現在屏幕上。 一切都運行良好,除了一件事,收集它們后,沒有硬幣從現場銷毀,但評分系統似乎運行良好,我真的不知道我做錯了什么。
硬幣的游戲對象是一個預制件,正在由場景中的另一個主要游戲對象加載。 有兩種 C# 腳本,一種用於 Score Text Script,另一種用於 Coin Script。 如果有人可以幫助我,我將不勝感激,謝謝!
這是硬幣腳本的代碼:
public class CoinScript : MonoBehaviour {
public GameObject PickUp;
void OnTriggerEnter(Collider col)
{
PickUp = GameObject.FindGameObjectWithTag("PickUp");
if (gameObject != null)
{
// Do something
Destroy(gameObject);
ScoreTextScript.coinAmount++;
}
}
}
這是分數文本腳本的代碼:
public class ScoreTextScript : MonoBehaviour {
Text text;
public static int coinAmount;
void Start()
{
text = GetComponent<Text>();
}
void Update()
{
text.text = coinAmount.ToString();
}
}
盡管您的問題很難閱讀,但我可以提出以下有關如何將其顯示給 UI 的解決方案:
1)創建事件通道對象和腳本
事件通道是在事件發生時通知類的觀察者
讓我們以我團隊的代碼為例,用它作為你硬幣的靈感
namespace Game
{
[Findable(R.S.GameObject.GameHasEndedFormallyEventChannel)]
public class GameHasEndedFormallyEventChannel : EventChannel
{
public event EventHandler OnGameEnded;
public override void Publish()
{
if (OnGameEnded != null)
{
OnGameEnded();
}
}
}
}
事件處理程序用於類,它們將“訂閱”事件並等待信號。 方法 Publish 是為那些將事件通知訂閱類的人准備的。
不要忘記將它添加到游戲對象(最好是所有其他游戲對象腳本都可以訪問)
現在在檢測玩家與硬幣碰撞的類中,通過找到它的游戲對象來實例化 EventChannel 並在碰撞后立即調用方法 publish()。
在 UI 類上,添加以下代碼(不要忘記在該類中也找到事件通道):
private void OnEnable()
{
playerGainedScoreEventChannel.OnCoinGained += YourMethodToUpdateScore;
}
在更新分數的方法中,您可以執行以下操作:
score++
scoreUI.text = score.ToString()
我希望這有幫助
正如阿德里安所說,盡管代碼數量有效,但 Destroy 調用也應該有效。 您確定在您的預制件中,您正在銷毀的游戲對象(放置腳本的游戲對象)與整個硬幣相同嗎?
這是這段代碼很少清理的示例:
public class CoinScript : MonoBehaviour {
//You don't use it, at least at OntriggerEnter, so you might not need it
//public GameObject PickUp;
void OnTriggerEnter(Collider col)
//You don't use this at this method PickUp at this method
//PickUp = GameObject.FindGameObjectWithTag("PickUp");
//gameobject is never null if it's not destroyed, u dont need this if.
//if (gameObject != null)
//if you want to be sure only PickUp gameObject is able to collect the coin you can do this
if(col.gameObject.tag == "PickUp")
{
// Do something
ScoreTextScript.coinAmount++;
Destroy(gameObject);
}
}
}
下面是使用 C# 的腳本
硬幣腳本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Coins : MonoBehaviour {
void Start () {}
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Triggered");
Destroy(gameObject);
scorescript.scorevalue += 3;
}
// Update is called once per frame
void Update () {
}
}
評分腳本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class scorescript : MonoBehaviour {
public static int scorevalue = 0;
Text score1;
// Use this for initialization
void Start () {
score1 = GetComponent<Text>();
}
// Update is called once per frame
void Update () {
score1.text = "score : " + scorevalue;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.