5

I am using DOTween pro asset from the Unity store along with the textmeshpro. Attached is a screenshot showing my setup:

enter image description here

There are few collectibles in my scene; I want to be able to run the animation with a different text each time when an item is collected.

Below is my code snippet:

DOTweenAnimation[] animations = pickUpTexts.GetComponents<DOTweenAnimation>();
for(int i=0;i< animations.Length; i++)
{
    Debug.Log("animations " + animations[0].animationType);
    if (animations[i].animationType == DOTweenAnimationType.Text)
    {
        textAnimation = animations[i];
    }
    if (animations[i].animationType == DOTweenAnimationType.Move)
    {
        moveAnimation = animations[i];
    }
}

later when item is collected, I am calling this:

textAnimation.endValueString = "New pick up collected, blah blah";
//textAnimation.DOPlay();
textAnimation.DORestart();

Basically, I am trying to change the endValueString of an animation and re-run the animation with the new value. When a pick up is collected, I can see the endValueString being updated in the inspector, but the Tween is still playing(or not) with the same old value. The screenshot showing the update is shown below: enter image description here

I tried changing using the Restart/Play, switched off the autokill option of the component, tried changing the loops counter in inspector, tried using textAnimation.DORestart(true) but nothing seems to have worked.

The solution that I have found so far is not to use the animation component as it is not re-usable and just use the following line in my script:

pickUpTexts.GetComponent<TextMeshProUGUI>().DOText("New pick up collected, blah blah", 1f, true).SetRelative(true)
TylerH
  • 20,799
  • 66
  • 75
  • 101
Vipin Verma
  • 5,330
  • 11
  • 50
  • 92

2 Answers2

9

You can't change the endValue of a DOTweenAnimation, but you can change the endValue of the tween it generates using ChangeEndValue:

// myDOTweenAnimation indicates a reference to the DOTweenAnimation component
myTween = myDOTweenAnimation.GetTweens()[0];
myTween.ChangeEndValue("something else");

SIDE NOTE: I'm the developer of the tool the OP is using.

Demigiant
  • 290
  • 2
  • 7
  • what is `myDOTweenAnimation` here? – Vipin Verma Jul 29 '18 at 16:43
  • @vipin8169 "myDOTwenAnimation" indicates a reference to the DOTweenAnimation that generates the text tween. – Demigiant Jul 29 '18 at 16:49
  • but how do I get the `myDOTwenAnimation` if the animation is attached to the component in inspector, but not created using the script alone? – Vipin Verma Jul 29 '18 at 16:50
  • You can store a reference to it somewhere (like as a public property/inspector-available of some object). Or if you know the gameObject (let's call it `myGameObject`) that holds the component, you can use `myGameObject.GetComponent()` – Demigiant Jul 29 '18 at 16:53
  • ok I see, I will try and get back to you. Thank you so much for your help :) – Vipin Verma Jul 29 '18 at 16:56
  • So, I resorted to using script only as I do not want to undo all the changes I did in last 24 hours. Now I am creating the tween for the first iteration only, and then later using the same object by just changing its endvalue as you suggested: `if (pickUpTweener == null) pickUpTweener = pickUpTextObj.DOText(pickUptext, 2f, true); else pickUpTweener.ChangeEndValue(pickUptext, 2f, false);` Moreover `pickUpTweener.SetRelative(true);` is not working in this case. – Vipin Verma Jul 29 '18 at 17:12
  • However, it works for first 2-3 iterations and then leading to the following exception: `DG.Tweening.Plugins.StringPlugin.SetChangeValue (DG.Tweening.Core.TweenerCore`3 t) (at D:/DG/_Develop/__UNITY3_CLASSES/_Holoville/__DOTween/_DOTween.Assembly/DOTween/Plugins/StringPlugin.cs:56)` – Vipin Verma Jul 29 '18 at 17:13
  • 2
    [This answer is being discussed on meta.](https://meta.stackoverflow.com/questions/371715/did-this-answer-require-the-op-to-disclose-affiliation) – Script47 Jul 30 '18 at 10:04
  • @vipin8169 About SetRelative not working, it's because I assume you're chaining it after changing the end value. Once a tween starts its parameters are "locked", so you can change only a limited set of them. Though if you have to change the end value often, maybe it would be better to just recreate the tween. Tweens have almost zero overlay, so it's not an expensive/GC-inducing operation. – Demigiant Jul 30 '18 at 18:06
  • @vipin8169 About your error instead, that might be a bug and I'd like to investigate it. Could you signal it in DOTween's github repo (where you found me the first time)? I'll be traveling tomorrow but after that I'll check it out. – Demigiant Jul 30 '18 at 18:06
2

Although @Demigiant's answer is correct, at version:

DOTween v1.2.335 [Release build] DOTweenPro v1.0.178

A cast is required because it returns a Tween object which does not implement the ChangeEndValue method.

So to get it to work as intended you need to cast it to a Tweener first:

// myDOTweenAnimation indicates a reference to the DOTweenAnimation component
myTween = myDOTweenAnimation.GetTweens()[0];
((Tweener)myTween).ChangeEndValue("something else");

PS. I could not comment it because I do not have enough reputation. Once the proper answer is fixed this can be removed.