- Home /
particle system effects other things in my game
i currently have a piece of code that destroys some gameobjects in a group and instantiates coins in their place, it looks like this
GameObject[] sensedObjects = GameObject.FindGameObjectsWithTag("THREE_BY_THREE_GROUP");
foreach (GameObject foundOne in sensedObjects)
{
GameObject objectMain = foundOne.transform.parent.gameObject;
GameObject newObj = GameObject.Instantiate(coins) as GameObject;
newObj.transform.parent = objectMain.transform.parent;
Vector3 lower = new Vector3(objectMain.transform.position.x,
objectMain.transform.position.y - 0.5f, objectMain.transform.position.z);
newObj.transform.position = lower;
newObj.transform.localScale = new Vector3(0.15f, 0.15f, 0.16f );
GameObject.Destroy(objectMain);
}
this works perfectly but what im trying to do now is when this happens (game object object main being destroyed and game object newObj being instantiated) is show a particle system looking like an explosion and disapearing after its finished so i made a particle system in the editor on my object main and tried to use the following code
GameObject[] sensedObjects = GameObject.FindGameObjectsWithTag("THREE_BY_THREE_GROUP");
foreach (GameObject foundOne in sensedObjects)
{
GameObject objectMain = foundOne.transform.parent.gameObject;
ParticleSystem ps = objectMain.GetComponent<ParticleSystem>();
ps.Play();
objectMain.GetComponent<Renderer>().enabled = false;
objectMain.GetComponent<Collider>().enabled = false;
GameObject newObj = GameObject.Instantiate(coins) as GameObject;
newObj.transform.parent = objectMain.transform.parent;
Vector3 lower = new Vector3(objectMain.transform.position.x,
objectMain.transform.position.y - 0.5f, objectMain.transform.position.z);
newObj.transform.position = lower;
newObj.transform.localScale = new Vector3(0.15f, 0.15f, 0.16f );
GameObject.Destroy(objectMain,ps.main.duration);
}
this has bad results the particle system plays, the renderers and coliders are off so that it disappears instantly and is destroyed after the particle system finishes, but the newly instantiated coins kind of flash with the particle system for a brief second and then is destroyed (isnt appearing in the scene hierarchy) im quite new to unity can anyone help?
This seems like an extremely convoluted way to accomplish a very simple task. What are the events the lead up to this destruction/instantiation?
Put a debug.log before destroying the gameobject and output the value of ps.main.duration. I suspect its a very small value, and is causing it to be destroyed in a fraction of a second.
@RobAnthem all objects are passed through once, when all objects in a group have been passed through they're all turned into (destroy / instantiate) into coins and as mentioned works perfectly without the particle system, @bobisgod234 I have printed a line to see it fire, and the particle system should be 1 second long but I haven't printed it to check so I'll do that and update thanks for your answers
I get that, but what I mean is, let's say this event is triggered by a collision, then almost all of this script is useless. Searching and iterating is something that should be done on extreme circumstances, what I want to know is what you are actually ai$$anonymous$$g to accomplish here. If an object hits these objects and they turn into coins, then you went about doing this wrong. If your particle system only triggers once per object, consider placing it on the coin and let it play on awake. Disabling the renderer and collider makes no sense, you are adding additional overhead to the simple task of disabling the object itself. Remember that FindObject and GetComponent calls are costly and should be avoided, actually there is almost never a reason to need them except in rare occasions.
To make matters worse, your system almost reflects object pooling, which is s good thing, but then you follow it up with a timed destroy, adding additional overhead and forcing garbage collection.
@RobAnthem I'm ok at program$$anonymous$$g so most of what you said makes sense but I'm near clueless with unity so your comment gives a small insight into expensive calls such as find so thank you, I don't quite understand why if this is a collision then all of this is useless, is that because your assu$$anonymous$$g this script will be on the object (it's actually on the player) but what you mentioned about putting the particle system on the coin and play in awake is exactly what I should be doing and what j will be doing going forward if you want to add that as an answer I'll accept it, many thanks for all your words