Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Damage Popup Issue

Discussion in 'Scripting' started by Flynn_Prime, Jun 16, 2017.

  1. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    383
    Code (CSharp):
    1. GameObject InitDmgText (string text)
    2. {
    3. GameObject temp = Instantiate (DmgTextPrefab) as GameObject;
    4. RectTransform tempest = temp.GetComponent <RectTransform> ();
    5. temp.transform.SetParent.(transform.Find ("EnemyCanvas"));
    6. tempRect.Transform.localPosition = Prefab.transform.localPosition;
    7. tempRect.Transform.localScale = Prefab.transform.localScale;
    8. tempRect.Transform.localRotation = Prefab.transform.localRotation;
    9.  
    10. temp.GetComponent <Text> ().text = text;
    11. Destroy (temp.gameObject, 2);
    12.  
    13. return temp;
    14. }
    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.
     
  2. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    One idea..

    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)?
     
  3. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    383
    Thanks for the reply.

    I had considered this, however the canvas is also used to display health above the enemies heads.
     
  4. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    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).
     
  5. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    383
    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
     
  6. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    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.
     
  7. bomal

    bomal

    Joined:
    Jul 3, 2018
    Posts:
    6
    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);