Check Color of colliding particle?
I want to make a top down space shooter. The bullets are particle.
On my Enemy Class i have made this:
void OnParticleCollision(GameObject other) {
life--;
if (life <= 0) {
Destroy(gameObject);
}
}
This working perfect. But one of my mechanic in this game is, to shoot bullets with different color. So the player can switch his bullet color.
Some Enemys are resisdent against a specific color. So I must check in the OnParticleCollision() which color the colliding Particle have.
void OnParticleCollision(GameObject other) {
life--;
//Check here which Color this Particle have
//<--- insert code
if (life <= 0) {
Destroy(gameObject);
}
}
Does somebody has any idea?
Answer by Mine54i__ · Mar 18, 2017 at 10:17 PM
void OnParticleCollision(GameObject other)
{
life--;
//Check here which Color this Particle have
if (other.GetComponent<Renderer>().material.color == /* color here */)
{
}
if (life <= 0)
{
Destroy(gameObject);
}
}
You will then input the color of the particle that you want detected. You would input something like Color.red, but make sure that if you do that, that the color that you have the particle is exactly the color you input, like Color.red, the particle color needs to be R: 255, B: 0 G: 0. I have not tried this yet, but it should work :)
Thanks, but Unity give me this error: $$anonymous$$aterial doesn't have a color property '_Color' UnityEngine.$$anonymous$$aterial:get_color()
Ok I changed the Particle $$anonymous$$aterial from "Default-Particle" to "Sprites-Default" and now there is no error. But now it returns 1,1,1. I think this is the color of the Sprite $$anonymous$$aterial without the color of the ParticleSystem :S
Hey! Sorry that I could not respond quicker, I had something going on. I think I figured it out. Is the color you set in the particle the start color? Or is it something else. If it is the start color that you set, this code will work.
if (GetComponent<ParticleSystem>().main.startColor.color == Color.red)
{
//Do the stuff here
}
That will detect what the start color is on the particle system. If you want to detect it on the collided object, you would change it to this.
if (other.GetComponent<ParticleSystem>().main.startColor.color == Color.red)
{
//code here
}
Other being the object that has the particle on it. Hope this helps! I tried it out and it works for me, but if it is not the start color that you set I will be happy to help you for whatever you have. If it is not the start color, if you could put the code of where you set the color or something that would be appreciated, then we could try and figure it out from there :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612113451im_/https://answers.unity.com/themes/thub/images/avi.jpg)