Using OnparticleCollision noob problem
Hi everyone, I am new to learning C# and trying to wrap my head round this. Any help is most welcome! :)
I am trying to use 'OnParticleCollision' to destroy an object and spawn two prefabs (explosionColliderPrefab and explosionPrefab) at the same time. However. I have a parsing error at line 9 and I can't see what it is and my two 'public GameObject' are not appearing inside of unity in the script Inspector window. Could anyone please point me in the right direction? Thanks in advance!
public class ParticleCollision : MonoBehaviour {
// explosion prefab
public GameObject[] explosionPrefab;
// explosion collision prefab
public GameObject[] explosionColliderPrefab;
}
// explode if hit by a particle
void OnParticleCollision(GameObject other)
if (explosionPrefab) {
// Instantiate an explosion effect at the gameObjects position and rotation
Instantiate (explosionPrefab, transform.position, transform.rotation);
}
if (explosionColliderPrefab) {
// Instantiate an explosion collider at the gameObjects position and rotation
Instantiate (explosionColliderPrefab, transform.position, transform.rotation);
}
// if game manager exists, make adjustments based on target properties
if (GameManager.gm) {
GameManager.gm.targetHit (scoreAmount, timeAmount);
}
// destroy self
Destroy (GameObject);
}
}
Ok worked a little more on the code. It now parses and I can add the public fields in the inspector. It doesn't seem to be causing objects to be destroyed by the particles....yet. I have collision set to 'world' on the Particle system but is there something I am missing?
public class ParticleCollision : $$anonymous$$onoBehaviour
{
public int scoreAmount = 0;
public float timeAmount = 0.0f;
// explosion prefab
public GameObject explosionPrefab;
// explosion collision prefab
public GameObject explosionColliderPrefab;
// explode if hit by a particle
void OnParticleCollision(GameObject other)
{
// exit if there is a game manager and the game is over
if (Game$$anonymous$$anager.gm) {
if (Game$$anonymous$$anager.gm.gameIsOver)
return;
}
if (explosionPrefab) {
// Instantiate an explosion effect at the gameObjects position and rotation
Instantiate (explosionPrefab, transform.position, transform.rotation);
}
if (explosionColliderPrefab) {
// Instantiate an explosion collider at the gameObjects position and rotation
Instantiate (explosionColliderPrefab, transform.position, transform.rotation);
}
// if game manager exists, make adjustments based on target properties
if (Game$$anonymous$$anager.gm) {
Game$$anonymous$$anager.gm.targetHit (scoreAmount, timeAmount);
}
// destroy self
Destroy (gameObject);
}
}
Answer by Tekksin · Sep 17, 2018 at 12:29 AM
just go into the particle's properties, and apply a subemitter. You can set subemitters to instantiate prefabs at birth, death, and collision (and manual, but don't ask me what that one means). ParticleSystem > Subemitter > Add the prefab. Make sure the prefab is a child of the main particle instantiating the new stuff.