タイトルの通りオブジェクトとプレイヤーが当たったらパーティクルを表示させたいです。
オブジェクトは複数のprefab化されている物から一つだけをランダムで選び、それをゲーム画面に呼び出されています。
↓prefab化されているオブジェクトにアタッチしているスクリプト
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class YasaiController : MonoBehaviour { 6 public ParticleSystem hosi; 7 public GameObject yasai; 8 9 void Start () { 10 hosi = this.GetComponent<ParticleSystem>(); 11 hosi.Stop(); 12 } 13 14 void OnCollisionEnter2D ( Collision2D other) { 15 if (other.gameObject.tag == "point") { 16 Destroy(yasai); 17 hosi.Play(); 18 } 19 } 20} 21
MissingComponentException: There is no 'ParticleSystem' attached to the "tomato(Clone)" game object, but a script is trying to access it.
You probably need to add a ParticleSystem to the game object "tomato(Clone)". Or your script needs to check if the component is attached before using it.
UnityEngine.ParticleSystem.Stop () (at /Users/builduser/buildslave/unity/build/artifacts/generated/common/modules/ParticleSystem/ParticleSystemBindings.gen.cs:4050)
YasaiController.Start () (at Assets/Scripts/YasaiController.cs:11)
とエラーが出たので検索してみましたが、解決方法が記載されていませんでした。
ご教授願います。
追記1
↓現在のprefab化されているオブジェクトにアタッチしているスクリプト
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class YasaiController : MonoBehaviour { 6 [SerializeField] ParticleSystem hosi; 7 8 void Start () { 9 Debug.Log(hosi != null ? "ParticleSystemが子要素にアタッチされている" : "PParticleSystemが子要素にアタッチされていない"); 10 hosi = GetComponentInChildren<ParticleSystem>(); 11 } 12 13 void OnCollisionEnter2D (Collision2D other) { 14 if (other.gameObject.tag == "point") { 15 hosi.Play(); 16 Destroy(yasai); 17 } 18 } 19}
↓tomatoにアタッチしているスクリプト、パーティクル
↓パーティクルの設定
追記2
tomatoに直接Particleをアタッチ。
↓tomatoにアタッチしているスクリプト
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class YasaiController : MonoBehaviour { 6 [SerializeField] ParticleSystem hosi; 7 public GameObject yasai; 8 9 void Start () { 10 hosi = GetComponent<ParticleSystem> (); //ここを追加 11 Debug.Log(hosi != null ? "ParticleSystemが子要素にアタッチされている" : "ParticleSystemが子要素にアタッチされていない"); 12 } 13 14 IEnumerator OnCollisionEnter2D (Collision2D other) { 15 if (other.gameObject.tag == "point") { 16 hosi.Play (); 17 18 yield return new WaitForSeconds (0.8f); //0.8秒待つ。しかし、この間はオブジェクトは削除されない。それを改善したい。 19 20 Destroy (yasai); 21 } 22 } 23}
"yield return new WaitForSeconds (0.8f);"これで0.8秒待ち、その間にパーティクルを出すことによってパーティクルを出すことができました。しかし、その0.8秒の間tomatoは削除されないのでそこを、tomatoがpointというタグのオブジェクトに当たったらすぐに削除&パーティクルを出すようにしたい。
ご教授願います。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/07/24 23:52
2018/07/25 06:27
退会済みユーザー
2018/07/25 07:11
2018/07/25 13:39
退会済みユーザー
2018/07/25 14:33
2018/07/25 14:42
2018/07/25 14:46 編集
退会済みユーザー
2018/07/25 15:07
2018/07/25 20:23
退会済みユーザー
2018/07/26 00:27
2018/07/26 13:26
退会済みユーザー
2018/07/26 14:51
退会済みユーザー
2018/07/29 00:50