Search Unity

Need help with floating damage text.. How do i change the values pre-Instantiated during live?

Discussion in 'Scripting' started by timhogan123, Mar 7, 2021.

  1. timhogan123

    timhogan123

    Joined:
    Aug 1, 2019
    Posts:
    5
    I trying to work with floating damage text. I can set it up relatively easy. The animation + text is on a game object of which is a child on a parent empty game object.

    Its Instantiated OnColission plays the animation of the floating text, an event on the Animation line calls a function that destroys the parent thus child after the animation is over.

    What i cant figure out is how to give it values before its Instantiated It only reads script that sit on this object. for example if my player picks up an attack dmg boost, i want to bump up the damage on the floating text, but how do i feed this new info to something that doesn't exist on the Hierarchy yet?

    A couple things come to mind i could try..

    Have multiple floating damage text prefabs that all have different values depending on the situation that are called depending on the situation, but that seems fairly inflexible.

    Scrap the Instantiating and focus on Playing the Animation while having a FloatingDamageText Manager game object that can change the values, but this seems awkward.

    Am i missing a really simple way to do this?

    Any help or suggestions would be really appreciated thanks.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    You can't give it values before it is instantiated, but you CAN Instantiate a thing and immediately give it a value.

    For this I like the Factory pattern, whereby you say "Make me a score object with 123 in it!"

    The essential steps would be to have a static function that:

    - loads a prefab, parenting into into your scene as necessary
    - reaches into the prefab to find the Text object (ideally with a direct link in a script on the base GameObject of the prefab) and manipulates the text field, filling it out.

    What that means is everywhere you want to make one of these things, it's just one call.

    Enclosed is a "digits" package that I use in a lot of my games. This particular one does NOT a UnityEngine.UI.Text object, but it could just as well be. You would just need to pass in a valid RectTransform and parent it under that with the Instantiate call, since all UI stuff must live under a canvas.

    Open the
    ztestonly_digits
    scene and click around to make digits.
     

    Attached Files:

    timhogan123 likes this.