- Home /
Play after collision a Particle-prefab and spawn a new Coin-Prefab
Hey guys.
I have now created a particle effect and ad it to a prefab. When I start the game the particle effect plays immediately without any collision. What I'm doing wrong here? EDIT: I have solved the first problem.
If the collider is destroyed, how I can spawn a new coin-prefab on the same position?
As last, how can I destroy a collider separately? Currently if I'm jumping between the 2 colliders both will destroyed. I would like it that only once per jump destroyed a collider.
I hope you can help me.
Here is the script(on the line 20 for particle effect):
using UnityEngine;
using System.Collections;
public class BlockBreakable : MonoBehaviour {
public float breakableTime = 0.5f;
AudioClip soundBumpBreak;
public string tag = "Player";
public PlayerControllerScript controller;
public Transform particleprefab;
public GameObject coinprefab;
void OnCollisionEnter2D(Collision2D coll)
{
if(coll.gameObject.tag == tag && !controller.grounded)
{
BreakableWait ();
Destroy (this.gameObject, breakableTime);
Instantiate(particleprefab, new Vector3(0, 0, 0), Quaternion.identity);
}
}
IEnumerator BreakableWait ()
{
yield return new WaitForSeconds(breakableTime);
}
void Start ()
{
audio.clip = soundBumpBreak;
audio.Play ();
}
}
Answer by SkaredCreations · Mar 22, 2014 at 07:21 PM
Use transform.position:
void OnCollisionEnter2D(Collision2D coll)
{
if(coll.gameObject.tag == tag && !controller.grounded)
{
BreakableWait ();
Instantiate(particleprefab, transform.position, Quaternion.identity);
Instantiate(coinprefab, transform.position, Quaternion.identity);
Destroy (gameObject, breakableTime);
}
}
However just to be clear, your BreakableWait function does nothing and also it isn't currently executed as coroutine because you're not calling it through StartCoroutine. You can just delete the line where you call it.
I know, I have already done it.
How can I destroy a collider separately? Currently if I'm jumping between the 2 colliders both will destroyed. I would like it that only once per jump destroyed a collider.