- Home /
Pop up text from Floating text positioner interface c#
Hello, this is my first time posting here, after reading for months ^^ I don't want to bother anyone, but i need help with this, since i am struggling for days.
I watched a video on youtube, where a guy makes a costum interface, didn't fully get the video, but followed the steps and created the costum interface, which allows me to see pop up damage, that fades away. Let me explain what the problem is here. I put the damage to pop up when i hit enemies, in the void, where damage is being done to enemies (EnemyHealthManager script C#). I put a pop up message when items are picked from the ground, or score is added. I can not make a pop up damage, when damage is being done to the Player, because, the place where i should put the code to work properly is in a static void.
Ok so here is the interface
using UnityEngine;
public interface IFloatingTextPositioner
{
bool GetPosition(ref Vector2 position, GUIContent content, Vector2 size);
}
In another script, there are some variables, which allow me to position the text in the scene, by creating a GUISkin.
A code that puts pop up damage in the game looks like this:
FloatingText.Show(string.Format ("{0}", damage), "DMGDoneToEnemyText", new FromWorldPointTextPositioner(Camera.main, transform.position, 2f, 50));
So when I put it in the script, where health is being reduced from the enemy, it works just fine:
public void damage(int damage)
{
Instantiate (hitEffect, monster.transform.position, monster.transform.rotation);
enemyHealth -= damage;
FloatingText.Show(string.Format ("{0}", damage), "DMGDoneToEnemyText", new FromWorldPointTextPositioner(Camera.main, transform.position, 2f, 50));
}
But when I put it, where health is being reduced from the Player, since it's a static method, and my current Health is also static it gives me an error:
public static void HurtPlayer(int damage)
{
FloatingText.Show(string.Format ("{0}", damage), "DMGToPlayer", new FromWorldPointTextPositioner(Camera.main, transform.position, 3f, 60));
curHealth -= damage;
}
I tried making the public static int curHealth; non-static, just public int curHealth;, and also tried making the public static void HurtPlayer, non-static. I tried leaving one of them static, i tried making them both non-static.. It just doesn't work, so i am sad now.
I read a lot of forum posts, on similar topics, but i don't quite understand what i need to do, in order to make it work. I'm sure once i find out, it will help me improve myself, and ofcourse i will stop being frustrated. I know it's a long post, but just wanted to be clear on what's happening.
Any help is greatly appriciated!
P.S. I know i can't call this function from a static method..
Unless there is a GREAT reason you aren't using Unity 4.6+'s UI system, you should be! GUI is legacy, except for editor scripting! Sorry to say, it appears you have followed an outdated tutorial. That doesn't mean it can't work or won't be educational, however.
When you say something gives you an error, tell us what that error is.
I got lost trying to follow what you're saying about why certain methods must or must not be static, etc. Researching for a stronger understanding of what the static keyword does is probably the best advice anyone can offer you. It seems as though you aren't quite sure what it means or when to use it.
Based on what I can see and what you've told us, I can't be sure why your approach isn't working.
When I wanted damage numbers to pop up in a project, all my objects that could receive damage implemented an IHealth interface somewhere in their script(s). That interface's method processed everything related to receiving damage, including damage resistance, applying additional effects like stun or knockback, and so on. It was also responsible for calling a singleton DamageText service which handled the creation of short-lived Text objects.
It is wise, when it's possible and convenient, to push any method calls you will frequently use identically into some kind of a system or service that does most of the work for you automatically "under the hood". The fact that your calls to this service are slightly different and work differently in different places seems odd to me. I'd recommend a strategy that ensures they are accessed the same and work the same regardless of the circumstances. Only objects that can receive damage can request the spawning of damage numbers, so why not put that request in a highly generic ReceiveDamage method?
Your answer
![](https://koobas.hobune.stream/wayback/20220613200633im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How To Make Floating Combat Text In new UI System of 4.6 1 Answer
converting from GUI text to GUI Labels 0 Answers
Can I do a "rotatory" UI like this? 1 Answer
GUI text font change 3 Answers
Limit on GUI Components? 0 Answers