C# scripting and Particle System incompatibility
I have a C# script titled Health.cs, which basically checks for the collision of the player with the enemy, if collided with enemy, it destroys the player prefab and simultaneously instantiates a prefab having a particle system attached to it. Now the enemy and the player both have different explode prefabs, and their are multiple players which can be selected from the menu GUI. My strategy is to change the Material of the Renderer in the particle system of the explode player prefab( remember I have two different prefabs as mentioned earlier) and replace it with the material of the current selected player, so as to give the effect that the player and the enemy have disintegrated on collision, through the Health.cs script. For easier purposes, I have created a public GameObject component for storing the reference of the explode player prefab. For some reason, the following options are not working properly and messing up things:
I tried to access the material of the currently selected player GameObject through the Mesh Renderer component using GetComponent method, store it in a private variable 'pr' and assign the value of pr to the renderer of the particle system of the GameObject by using the code "explosionPrefab.GetComponent().material = pr ;"
For the second time, I converted the private Material pr to public, and separately assigned the materials for all individual players. Along with the first code snippet, I also tried instead of .
No matter which way I use, the explode player Prefab's particle system renderer gets some other material assigned to it, hence during collision, the explode player Prefab has a different material than the player's . I can't seem to understand what is going wrong, tried to find answers, searched for it but to no avail. So i sincerely need help for this. I have attached the code as a .txt file:-
Answer by FortisVenaliter · Oct 03, 2017 at 10:22 PM
Why not just make two prefabs? One for player explosion, one for enemy explosion, and just instantiate the correct one at runtime? That way you set it all up ahead of time, so no room for error.
I do have two different explosion prefabs, the problem is occurring only in the explode player prefab where the wrong material is getting assigned to it by script. Please check the attachment for the code.
Right, but if you have two prefabs with their materials already set, why are you setting the materials again?
Your answer
Follow this Question
Related Questions
Having Player light torches Using Particle System and OnCollisionEnter? 1 Answer
Has anyone found a way to change Shuriken particles with c# while it is in transit. 1 Answer
Particle Collision between particles 2 Answers
How Play and Stop a ParticleSystem through Scripting? 9 Answers
How to Instantiate decals where particles from particle system hits? 1 Answer