This is the code I’m using to generate floating popups of damage dealt to enemies. Problem I have is when the enemy is despawned back to pool there is no canvas, and hence nowhere for my floating damage to instantiate. This is all well and good, but for this reason the last hit dealt to an enemy before it dies does not show. Could anyone please advise a work around? Many thanks.
I assume the canvas is a component of, or child of your enemy. What about just having it on an empty game object instead and instantiate (or pool) the canvas when a hit happens, and base the canvas location on the location of your enemy (or an offset location from the enemy of some sort)?
Well you could separate it into it’s own canvas… or really just pull that whole canvas into an empty object, and in it’s update you can move it with the enemy. Just have a variable within this new object that stores the instance of whatever it’s following.
If you really absolutely have to keep things all with your enemy object, then just delay the destroying of your enemy object… perhaps disable it’s collider, and have some death animation, or also disable rendering of the object… whatever makes sense… and then destroy everything after 2 seconds (or however long it takes for your damage popup to display).
Funnily enough the first thing I tried was to add a delay of sorts. I figured once I add death animations it would work quite well. But I couldn’t get it to work. Not sure why. I had a kill function that simply despawned the enemy, so I changed it to a coroutine instead and added Wait for seconds. All of the damage popups were shown but there enemy didn’t Despawn. I’ll keep messing around with it and shown if I can get it to work
Make sure that your destroy is destroying the parent (i.e your enemy) game object instance as opposed to just destroying a script or your canvas, or whatever other component it might be accidentally destroying instead (you could tell probably by inspecting your enemy in the hierarchy/inspector and seeing if something else disappears off your enemy). Explicitly tell it to destroy your enemy game object instance. Something like “Destroy(this)” won’t do that.
Edit: Oh sorry, i forgot you mentioned you use pooling… but similar concept, anyway.
I know that this is a bit old post, but I think that when pooling, you should only deactivate the GameObject and not destroy it.
gameobject.SetActive(false);