Search Unity

How can I make a multiple cloned prefab with animation working?

Discussion in 'Animation' started by Patta96, Oct 18, 2019.

  1. Patta96

    Patta96

    Joined:
    Nov 17, 2017
    Posts:
    4
    Dear lovely community,

    I'am creating a 2d game and I would like to add a floating damage pop up text every time I click on a certain object. For this I clone a prefab with a text popup animation every time I click on the screen. The prefab is put into the game as intended however the animation isn't working. When I put only one animation into the screen it works. I don't simply don't know why it isn't working... So how can I clone a prefab with an animation and make it working with other animations of the same prefab already running? This is driving me crazy and some help would awesome!

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Enemy : MonoBehaviour {
    4.     public int health;
    5.     void Start()
    6.     {
    7.         FloatingTextController.Initialize();
    8.     }
    9.  
    10.     public virtual void TakeDamage(int amount)
    11.     {
    12.         FloatingTextController.CreateFloatingText(amount.ToString(), transform);
    13.         Debug.LogFormat("{0} was dealt {1} damage", gameObject.name, amount);
    14.         if ((health -= amount) <= 0)
    15.             Die();
    16.     }
    17.  
    18.  
    19.     public virtual void Die()
    20.     {
    21.         Debug.Log(gameObject.name + " has died.");
    22.     }
    23.  
    24.     void OnMouseDown()
    25.     {
    26.         this.TakeDamage(Random.Range(0, 100));
    27.     }
    28.  
    29. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class FloatingText : MonoBehaviour {
    6.     public Animator animator;
    7.     private Text damageText;
    8.  
    9.     void OnEnable()
    10.     {
    11.         AnimatorClipInfo[] clipInfo = animator.GetCurrentAnimatorClipInfo(0);
    12.         Debug.Log(clipInfo.Length);
    13.         //Destroy(gameObject, clipInfo[0].clip.length);
    14.         damageText = animator.GetComponent<Text>();
    15.     }
    16.  
    17.     public void SetText(string text)
    18.     {
    19.         damageText.text = text;
    20.     }
    21. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FloatingTextController : MonoBehaviour {
    5.     private static FloatingText popupText;
    6.     private static GameObject canvas;
    7.  
    8.     public static void Initialize()
    9.     {
    10.         canvas = GameObject.Find("Canvas");
    11.         if (!popupText)
    12.             popupText = Resources.Load<FloatingText>("Prefabs/PopupTextParent");
    13.     }
    14.  
    15.     public static void CreateFloatingText(string text, Transform location)
    16.     {
    17.         FloatingText instance = Instantiate(popupText);
    18.         Vector2 screenPosition = Camera.main.WorldToScreenPoint(new Vector2(location.position.x + Random.Range(-.2f, .2f), location.position.y + Random.Range(-.2f, .2f)));
    19.         //Vector2 screenPosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    20.         instance.transform.SetParent(canvas.transform, false);
    21.         instance.transform.position = screenPosition;
    22.         instance.SetText(text);
    23.         instance.GetComponent<Animator>().Play("PopupText");
    24.     }
    25. }
    26.  


     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    Could create all needed popups at scene initialization to cache them, then simply use pooling approach, to grab relevant 'spare' popups. And when done, 'put back' to pooling cache / mark as spare.
     
  3. Patta96

    Patta96

    Joined:
    Nov 17, 2017
    Posts:
    4
    The problem is, that my game is gonna be some kind of clicker game. So I simply don't know how often I have to click and the popup text itself will change as well.
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    It dosent matter. Instead of copying (instantiating popup GameObject?), you just use reuse existing one, which is not used. Similar technique, as for shooting many bullets.