Turn particle effect on, on collision

I have a cube with a smoke particle effect parented to it.

I want the particle to play when it collides with a tag group called “Obstacles”? How would i do this?

I’m new to C# still learning.

You would use this code here to detect the collision so it would be something like.

public GameObject particleEffect;


void OnCollisionEnter ( Collision other )

{
   if (collision.gameObject.tag == "Obstacles")

   {
         particleEffect.SetActive ( True );
   }
}

Here’s the basic idea or alternatively, use instantiate, or perhaps just play the particle effect by itself, up to you.

1 Like

Would I need to put it in my playerOnCollision script? or different script?

The script could be on your player or your obstacles. Up to you… You may have to modify the tag check, depending which you choose, of course. :slight_smile:

1 Like

Thank you both for your help, C# is so confusing.

I’ve tried to apply it to my player, and put the particle system into the public box,

But still getting errors

{

public GameObject particleEffect;

void OnCollisionEnter ( Collision other )

{
if (collision.gameObject.tag == “Obstacle”)

{
particleEffect.SetActive ( True );
}
}
}

The errors are saying this (there are two of them)

Assets/Scripts/Crashed.cs(12,7): error CS0103: The name collision' does not exist in the current context Assets/Scripts/Crashed.cs(15,31): error CS0103: The name True’ does not exist in the current context

Oh my bad! I think it should be other.gameobject.tag not collision.

1 Like

You probably just copied this script into a file without the proper using statements. Create a new script file and replace only the start and update methods in there with these except for the boundary {}.

Also if you post code in the forums, please, use the code tags or icons in the post editor.

1 Like

It’s still not working??? No errors?

I’m so confused??? I put it in the new script and applied my particles to it and changed the tag to what it is supposed to be to correspond to the tag on the obstacles… deactivated the particle system and played also tried it with it activated? still dosnt play? maybe it has to have another line for play or something?

Do you get any error messages? Could you please post the script you currently have (with code tags, so we can check out)?

I’ve got it to work, I had play on awake turned off, THANK YOU Guys! :smile:

Answer.

using UnityEngine;
public class Crashed : MonoBehaviour
{
public GameObject particleEffect;

void OnCollisionEnter(Collision Start)
{
if (Start.gameObject.tag == “Obstacle”)
{
particleEffect.SetActive(true);
Debug.Log(“We hit something Start the particles”);
}
}
}

Thanks guys :slight_smile: now im going to have a go at playing a sound as well.

1 Like

Please learn to use tags when so people can read your code easily, glad you got it sorted out, if there aren’t any errors and you know the code is correct it is usually a daft issue like what you described that can cause trouble.