RPG damage text not updating

Hey Everyone,

I’m working on a turn based combat system for my game while using some of the RPG game elements from the videos… Everything is working great up until I hit an enemy and the battle text spawns but does not update to the current damage done. It’s probably worth noting that my player’s attack is initiated in a coroutine from my battle system which then causes the animation to run and the damage to be done to the enemy. Attached are the damage text and spawner scripts as well as the event system on the enemies health. Everything seems to be fine but I do notice when I try to debug the area where I set the value of the spawned damage text that it never prints the debug string… I’m not sure if maybe it’s because the attack and thus damage taken is starting in a coroutine? Is there anything in this code that looks like it wouldn’t work?

Any help is greatly appreciated!

As near as I can tell everything looks right (It’s really much easier if instead of posting screenshots of your code (often difficult to visualize context), you copy and paste your code, select that code and click the </> format button.
Let’s add some debugs to make sure things are hooked up correctly (You’re on the right track with that debug at the end, but there are some missing in the middle that might cause us to investigate other causes). When Debugging, I like to do what I call a Debug Chain… I want to visualize the code moving from one component to another…

In what I’m assuming is Fighter.Attack() your Attacking statemement in the 2nd block, right after float num=…; add:
Debug.Log($"{name} is doing {num} damage to {combatTarget.name}");

Then in Health.TakeDamage, let’s make sure we’re taking the damage:
after healthpoints = Mathf.Max…;
Debug.Log($"{name} has taken {damage} points of damage, leaving it's health at {healthPoints}");
Now whether or not the character dies, we probably want the text to spawn for that final kill. Let’s delete that else statement.

Our debug chain in the inspector should now look like this:

Did the player move?
Attacking!
Enemy is doing x damage to Player
Player has taken x points of damage, leaving it's health at y
Spawned damage text

Hi,

So I’ve just implemented those debug lines in the code and those are perfect and get called up until the one in the damagetextspawner where it prints “Spawned damage text!”. I’d be happy to post the code to try and figure this out but since there’s quite a few scripts not sure which one to start posting :wink: it’s strange because the damage text instantiates and runs through its animation and gets destroyed but it just doesn’t get updated with the damage from that Invoke event from health. After seeing these debugs it’s quite clear that it’s possible for other scripts to fire off during a coroutine…

The coroutines shouldn’t make much of a difference… in fact, if you’ve seen my game Beneath, you’ll see DamageTexts all over the place… the entire game is a series of Coroutines, very few Update calls.

I don’t see the “Spawned Damage Text” in the logs… which would lead me to believe that something’s not hooked up right, except that you’re saying that the damagetext spawns, it just doesn’t update.

I can’t tell from this Console paste if you have any errors or not in the console. If you have the Errors unchecked, you won’t see them in the console display, only an added number in the upper right hand column of the console for errors… so… the next question, even though it doesn’t appear there are any in the console is “are there any errors in the console”?

haha nope, no errors either :frowning: I know I’m at a loss too right now, it instantiates but with the number I set in the text component. It made me think my prefab was setup wrong and maybe the text wasn’t connected but I paused the game and went into scene view to check the field and it was accurate :frowning: I am in 2020.1.4f version of unity. Another thing to note is all my other UI updates correctly such as tooltips for my attack options and inventories

I’m really curious why I’m not seeing a Spawned damage text in the logs… Run this one more time with but turn off collapse in the Console. Also… for now, comment out the extraneous debugs that sound like they’re already working (“should be running”, “Player Reached Enemy”, etc… the only ones we’re interested in right now is the chain from Attacking to the Damage Text.
(Part of using Debug.Logs for debugging is turning debugs off when we know we’ve gotten that part figured out, I"m guilty as charged when it comes to not doing so))

yeah that’s why I’m stuck here as well because since it instantiates it is technically ignoring that print statement before it and the .setvalue after it. Yep just took out all those other print statements and turned off collapse and same result it’s only showing the two debug statements you presented above :frowning:

Ok, now we’re going to go into “Faroutsville”… looking for a very strange bug that happens from time to time…
Find the DamageTextSpawner script in your assets folder, and select it but don’t double click or try to rename it…
Then look in the Inspector and see if the text in the script matches the text in Visual Studio

dang okay wow! That actually was it, I went into the component double clicked the script and it somehow pulled up a different version of what I was working in?? very strange! umm okay so now it’s saying the string format is incorrect error on this line

everything else is finally firing off :smiley: just freezes now on this

damageText.text = String.Format("{0;0}", amount);

got it! lol had to be a : lol
damageText.text = String.Format("{0:0}", amount);
that’s what fixed the freeze :slight_smile:

Thank you for that very incredibly weird bug fix!! :smiley: I would’ve given up lol

Sometimes a script gets “locked”… I have had this happen before… I’ll save and save and nothing changes in Unity… I wind up having to copy all my changes, close the file in Visual Studio, open the file again and paste the changes back in.

String.Format sucks anyways…
I use this:
damageText.text = $"{Mathf.RoundToInt(amount)}";

ooh that’s a much better method, that string formatting one was always strange to me this one makes much more sense!

It’s not generally covered in the courses because it’s a fairly new thing in Unity (even though it’s been in the C# standard since 7.0, Unity only adopted C#7.0 in the last couple years). I’ve been teaching it through my TA answers. :slight_smile:

Did you mean C# 6 (.NET 4.6)?

Oh, you’re right, it’s 6… I just remember when it came out, it didn’t fly in Unity at first. I didn’t try it in Unity again until v 2019 came out. Since then, I’ve used it religiously

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms