マウスクリックして消えたオブジェクトにエフェクトを反映させたい

実現したいこと

マウスクリックして消えたオブジェクトにエフェクトを反映させたい
エフェクトは数秒後に消えるようにしたい

前提

ゲームオブジェクトをクリックして消えたらスコア加算するようなゲームを作っているのですが
消した後にエフェクトを付けたいと考えパーティクルを作成しました。
スプリクトにパーティクルを紐付けクリックして消えたゲームオブジェクトに
反映させようとしましたがクリックして消えたオブジェクトとは別の場所にエフェクトが発生しました。
エフェクトを消したオブジェクトの場所に発生させるにはどうすれば良いのでしょうか?

発生している問題・エラーメッセージ

using System.Collections; using System.Collections.Generic; using UnityEngine; public class TouchDeleteScript : MonoBehaviour { GameObject clickedGameObject; public SystemMain Sm; //ヒエラルキーのSystemMainと紐づける private int Status; //準備ができたかどうかを判断する変数 public GameObject ExplosionEffect; // 爆破エフェクト(プレハブ) // Start is called before the first frame update void Start() { //SyatemMainを探す Sm = GameObject.Find("SystemMain").GetComponent<SystemMain>(); } // Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) { clickedGameObject = null; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit2D hitSprite = Physics2D.Raycast((Vector2)ray.origin, (Vector2)ray.direction); if (hitSprite == true) { clickedGameObject = hitSprite.transform.gameObject; if (clickedGameObject.tag == "mato") { Sm.Score += 1; //スコア加算していく数字 Destroy(clickedGameObject); // 爆破エフェクト Instantiate(ExplosionEffect, transform.position, Quaternion.identity); } } } } }

該当のソースコード これはSMと紐づけしているスコアのコードです。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; //UIを使用しているため、忘れずに記入

public class SystemMain : MonoBehaviour
{
public int Score; //Score変数を定義
//スクリプトをアタッチした時にスコア加算したいTextと紐づける
public Text ScoreText;

void Start() { Score = 0; //スタート時の表示 } // Update is called once per frame void Update() { ScoreText.text = string.Format("{0}", Score); //Textのフォーマット }

}

試したこと

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

unity2022.3.6f1

コメントを投稿

0 コメント