- Home /
How can I add text at a point where an enemy dies?
I haven't slept in 2 days pretty much so I'm finding it hard to think. I'm also only a beginner really.
I have a 2D game, and when you kill an enemy, it adds to your current gold. How would I have a 'popup' of the gold gained where the enemy died?
Say, you kill an enemy, it explodes, and right where it exploded, it would show "+[gold]". That +[gold] would disappear after about a second or two, and it would do it each time an enemy died. I have the gold earned on kill stored in an int, I just don't know how to get the position and how to make it disappear after a second and stuff.
edit: I wanted to head along the lines of using the GUI. Well I think that might be the only way so yeah. I don't know. I can't think I'm so tired.
Answer by BiG · Nov 25, 2012 at 03:45 PM
Personally, I use the following method:
Create a GUIText for the popup message (say "+ 100 Gold"). I assume the text to be static for semplicity, but you can modify the content of the text at runtime;
Destroy the popup message after a certain amount of time, with a script like this (attached to the GUIText):
var destroyTime=5;
function Start () {
yield WaitForSeconds(destroyTime);
Destroy(gameObject);
}
If you want to add a good effect, make also the message move vertically, before destroing it:
var speed = 90;
function Update () {
var y=Time.deltaTime*speed;
transform.Translate(0,y,0);
}
Now, make that GUIText a prefab (I will call it "Score_Text";)
Now, the interesting part: let's make the enemy die and show the respective gold:
var life=10;
var score_e=100;
var Score_Text : Transform; //This is the GUIText prefab that we created a bit ago
var scoreScaling : Vector3; //It's better that the message will appear a little shifted, respect to the enemy, say Vector3(5,0,5)
function OnCollisionEnter (theCollision : Collision) {
var player_damage : int;
if(theCollision.gameObject.tag == "Player"){
life -= player_damage;
if (life<=0){
GameObject.Find("Score").GetComponent(UpdateScore).score+=score_e;
Instantiate(Score_Text, transform.position+scoreScaling, transform.rotation);
Destroy(gameObject);
}
}
}
The script above is a complete example of a "killing-enemy" one, Obviously, it's just an example, and the interesting part, for you, is the line:
Instantiate(Score_Text, transform.position+scoreScaling, transform.rotation);
I hope that you will find this a bit useful to start.
Right, that's too much for my sleep deprived $$anonymous$$d to handle right now, but the first two bits look good. I was going to make that little effect as well, so you just saved me some time thankyou. I'll take a look at this tomorrow and get back to you, thanks for that.
I've tried everything with that script, and it keeps on giving me the error: 'UnassignedReferenceException: The Variable goldText of 'bulletScript' has not been assigned.'
'goldText' is the prefab name and the variable name, same as your Score_Text. Why wont this work? I even tried assigning it in the inspector and it still gives me that error.
Have you assigned the last script to the enemy AND drag 'n dropped the GUIText prefab in the enemy's Inspector, on the goldText variable? From the error, it seems that you have a "blank slot" in the goldText value, somewhere.
Basically, GUIText position can be interpereted as relative position across the screen - this means a position of something outside of the (0,0) -> (1,1) rectangle is most likely not going to be on your screen. Since your enemies are probably not dying within this particular area, and are most likely not perfectly matched to screen position when they do, spawning it in based on the dead unit's position directly is probably nto what you want. 3D text can be used like you're trying to use this - though you may have to make sure it faces the camera (been a while since I used them, I can't recall). Alternatively, you can try using Camera.main. WorldToScreenPoint to figure out where on your screen the dead unit is, and then use unity GUI calls to draw your text there.
Answer by Griffo · Nov 28, 2012 at 08:42 PM
OK I'm going to tell you how I do it, when I kill an enemy the text zooms into the camera fading as it does.
1 - Create an empty game object, I call it "TextContainer" and child it to you enemy then attach the first script below to it.
2 - Now create a 3D Text and child that to the one you've just made ( TextContainer ) I call this "Text" and attach the second script below, fill in the Text Mesh field in the inspector with your font and text .. ect .. When I import fonts I set there font size to around 90 so in the inspector set the scale to X-0.02 Y-0.02 Z-0.02 and in the Text Mesh set Character Size to 0.1, Anchor to Middle Centre and Alignment to Centre.
3 - Now in the Inspector of the TextContainer turn off the game object (un-tick, top left corner)
4 - Now on you enemy add this variable
var textContainer : GameObject;
Also add this function and call it when his/her life reaches 0
function destroy(){
textContainer.gameObject.SetActive(true);
yield WaitForSeconds(3.0); // Wait for 3 seconds then destroy the gameObject and its children
Destroy(gameObject);
}
5 - Now choose your enemy and in the Inspector in the Text Container slot drag the TextContainer game object you made in step 1 .. Hope this helps you out, and you may need to alter as it suits you.
1
#pragma strict
function Awake(){
}
function Start () {
}
function Update () {
transform.position = Camera.main.transform.position + Vector3(0,0,0) + Camera.main.transform.forward * 1;
transform.rotation = Camera.main.transform.rotation;
}
2
#pragma strict
var textColor : Color;
private var runOnce : boolean = true; // Use to run Coroutine once
private var originalScale : Vector3;
function Awake(){
}
function Start(){
renderer.material.color = textColor;
renderer.material.color.a = 1.0;
}
function Update(){
renderer.material.color.a -= 0.1 * Time.deltaTime * 5; // Fade away text, last number dictates how fast to fade, higher = faster
StartCoroutine(LerpScale(2.0f)); // Time to take to Lerp to new size in seconds
}
function LerpScale(time : float){
if(runOnce){
var targetScale = originalScale + Vector3(0.15, 0.15, 0.15); // New size to expand text to
var originalTime = time;
while (time > 0.0f)
{
time -= Time.deltaTime;
transform.localScale = Vector3.Lerp(targetScale, originalScale, time / originalTime);
yield;
}
}
runOnce = false;
}
I just solved this 5 seconds before you posted this, damn :/ Thanks for the effort though..
Answer by jheiling · Nov 25, 2012 at 03:38 PM
Camera.WorldToScreenPoint gets you the position of an object in screen space. To destroy an object after some time simply use Destroy.
Confused. Could you give me a quick example? I looked at the document and don't really understand. Won't need anything too complicated.
And what do you mean about destroying the object? What object would I be destroying? The text? I thought that wouldn't let the gold pop up anymore though
camera.WorldToScreenPoint(someTransform.position) will give you the screen position of someTransform. Destroy(someObject, time) will destroy someObject in time seconds. Of course you would destroy the object displaying the text...
I can't use that. It says I need a camera attached to the object.
Your answer
![](https://koobas.hobune.stream/wayback/20220613084458im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Checking to see if a position is occupied by a GameObject 2 Answers
How to Move an NPC from anyplace to a specific place in 2D? 1 Answer
How to get the position that is closest to a target within a given radius 0 Answers
How do I make my camera follow the player without rotating in 2D? 2 Answers
Moving child and parent 1 Answer