- Home /
Changing a material on a particle on collision
So basically I want to change the particles materials colour on my prefab when it hits the transform with tag Shield and I've tried just about everything I can think of but I cant seem to get at the right spot. here's my code at pretty much everything I have tried any help would be amazing hope I was at least somewhat close to solving somewhere. so in the image attached I want the green tint colour to be changed to lets say red on collision and only the one prefab that collided with the game object not all bullets turning red. any help would be absolutely amazing
public Material myColor;
public bool armed = false; // when true it will explode on impact
void OnCollisionEnter (Collision col)
{
if (col.transform.tag == "Player") {
Destroy (gameObject);
// start explosion
// decrement player health
} else if (col.transform.tag == "Shield") {
armed = true;
print ("armed " );
// print (GetComponentInChildren ().name); // ParticleSystem ps = GetComponentInChildren ();
//Material pm = getcom GetComponentInChildren<Material> ();
//print (pm.name);
// print (ps.GetComponent().material.GetColor("_TintColor").ToString()); // Renderer r = ps.GetComponent (); // r.material.SetColor ("_TintColor", new Color(1f,0f,0f,1f)); // print (ps.GetComponent().material.GetColor("_TintColor").ToString());
//GetComponent<Renderer> ().material.SetColor("_TintColor", new Color(1f,0f,0f,1f));
//Material[] m = ps.getcom ();
//print ("mmmm " + m.Length);
//print (GetComponentInChildren<ParticleSystem> ().GetComponent<MaterialPropertyBlock>());
//gameObject.GetComponentInChildren<ParticleSystemRenderer>().material = newMat;
Please edit your question and more carefully format your code. You can use the "insert code" button (the icon for which is alternating ones and zeros), to do it pretty easily.
Answer by TBART82 · Oct 11, 2017 at 07:07 AM
You need to access the 'material' component in the 'ParticleSystemRenderer'. Here's a quick script I wrote up. This should do the trick!
// The particle system you want to change
public GameObject particle;
// The new material you want to apply to the particle system
public Material newMaterial;
private void Start()
{
// Access the 'ParticleSystemRenderer' component, and change the material to 'newMaterial'.
particle.GetComponent<ParticleSystemRenderer>().material = newMaterial;
}
Thanks! $$anonymous$$eep program$$anonymous$$g and never give up!
Your answer
Follow this Question
Related Questions
Pong game collision problems 1 Answer
OnParticleCollision is broken 1 Answer
A little problem in partciles 2 Answers
Particles/Multiply (Double) shader with shadows 0 Answers
Moving individual particles? 0 Answers