Place popup-score at enemy position

Hi,

I’d like to place a popup-score at the position where an enemy has been destroyed. I’m developing a 2D-game in Unity3D.

So far the popup-score gets rendered inside of the canvas but the position seems to have an offset. How do I place the popup-score at the position of the destroyed enemy exactly?

This is the c#-class that fades the score:

public class FloatAndFade : MonoBehaviour {

Text text;

public float fadeDuration = 2.0f;
public float speed = 2.0f;

void Start () {
   text = this.GetComponent<Text>();
   StartCoroutine(Fade());
}

public IEnumerator Fade () {

   float fadeSpeed = (float)1.0 / fadeDuration;
   Color c = text.color;

   for (float t = 0.0f; t < 1.0f; t += Time.deltaTime * fadeSpeed) {
       c.a = Mathf.Lerp(1, 0, t);        
       text.color = c;
       yield return true;      }

   Destroy (this.gameObject);
}

void Update() {
   this.transform.Translate(Vector3.up * Time.deltaTime * speed);
   }
}

This is the FloatScore-method that places the popup-score inside of the canvas:

void FloatScore () {
   GameObject popupScore = (GameObject)Instantiate (floatScore);

   popupScore.transform.position = Camera.main.WorldToScreenPoint(this.transform.position);

   popupScore.transform.SetParent (canvas.transform, false);
}

These are the settings for the canvas:

This is the gameplay with the popup-score:

try to determine values of offset, it can be anchors or something
try to set popupScore anchoredPosition instead of position
try to setParent before setting anchored position

worldToScreenPoint returns values from 0,0 to screen size , where 0,0 is left bottom corner, but in your case 0,0 can be at center of canvas. It difficult to say without a project

Use World Space for the Text’s Canvas, then it will use the scene’s coordinate system. And parent the Canvas to the enemy at when you Instantiate it.

Thanks for the tips. I found this tutorial and now it works like a charm: