I have script that is supposed to clone a canvas prefab whenever a enemy is hit, but when it clones it always clones to the same position, the canvas and text are both centered at (0,0,0). Here are the scripts, the first script controls the collision interaction and cloning, the second controls the floating of the text, I feel like the second one has a issue, but I cant find a solution.
Script 1: Prefab cloning
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HurtEnemy : MonoBehaviour {
public int damageToGive;
public GameObject damageBurst;
public Transform hitPoint;
public GameObject damageNumber;
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Enemy")
{
//Destroy(other.gameObject);
other.gameObject.GetComponent<EnemyHealthManager>().HurtEnemy(damageToGive);
Instantiate(damageBurst, hitPoint.position, hitPoint.rotation);
GameObject clone = (GameObject)Instantiate(damageNumber, hitPoint.position, Quaternion.identity);
clone.GetComponent<FloatingNumbers>().damageNumber = damageToGive;
Debug.Log(hitPoint.position);
}
}
}
Script 2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FloatingNumbers : MonoBehaviour {
public float moveSpeed;
public int damageNumber;
public Text displayNumber;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
displayNumber.text = "" + damageNumber;
transform.position = new Vector3(transform.position.x, transform.position.y + (moveSpeed * Time.deltaTime), transform.position.z);
Debug.Log(transform.position);
}
}