- Home /
Instantiated clone GameObject spawning at wrong position.
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);
}
}
Answer by upasnavig90 · Feb 16, 2018 at 06:34 AM
Where you assigning hitPoint?? Instantiate(damageBurst, hitPoint.position, hitPoint.rotation); what is the value of hitPoint, if you are not assigning it, it will take default value.
the hitPoint is a point that is a child to the sword and its only purpose is to change the location on the sword the prefab clones. and the value reads out from the Debug of the hitPoint as, (76.2, -33.9, 0.8) in the first script while in the second script the Debug of the transform.position reads out initially as, (0.0, 0.0, 0.8)
Answer by nwmarkovic · Feb 16, 2018 at 05:17 PM
When I Debug.Log(hitPoint.position) before and after the Instantiate the location is the same (76.2, -33.9, 0.8), so I don't know if the issue even lies in the code or in the prefab that is being cloned.