Prefab always instanciated at 0
Context: I'm making a 2D rpg and I've got an issue with damage numbers(numbers that should appear where the weapon hits an enemy and then float up and disappear after a few updates).
Damage number is a canvas prefab with a child text gameobject; To create the damageNumber I'm using this command:
var clone=(GameObject)Instantiate(damageNumber,hitPoint.position,Quaternion.Euler(Vector3.zero));
where damageNumber is the canvas with the damage numbers,and hitPoint is an empty gameobject child of the weapon from which damage numbers should spawn,but instead they keep spawning at x:0 y:0 z:0 and then float up.
Here's the script that makes the numbers float up(attached to the canvas)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FloatingNumbers : MonoBehaviour {
public float moveSpeed;//speed at which the number goes up
public int damageNumber;//damage number shown
public Text displayNumber;//refers to the text box child of the canvas
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
displayNumber.text = "" + damageNumber;//shows damage number in the textbox
transform.position=new Vector3(transform.position.x,transform.position.y+(moveSpeed*Time.deltaTime), transform.position.z);//makes the number go up.I think that the problem may be here;
}
}
Sorry for bad English I'm not a native speaker
public GameObject prefab;
public Transform spawnPosition;
void Start(){
var clone = (GameObject)Instatiate(prefab, spawnPosition.transform, Quatinion.identity);
}
or
public string PrefabName;
public Transform spawnPosition;
private GameObject prefab;
void Start(){
go=Resources.Load<GameObject >(PrefabName);
var clone = (GameObject)Instatiate(prefab, spawnPosition.transform, Quatinion.identity);
}
also:
your next part should be fine from what i can tell myself If floating up if your goal, But where it spawns is incorrect. Vector3.zero is = to 0x,0y,0z and i think "Euler" is world space corect me if i'm wrong That's your error isn't it 0,0,0, of world space. This isn't what you want, you want the local space of where the player is or object rather that is being damaged. and have it spawn aka appear above their head and then float up and out of the screen and then destroy.. Possibly making it fade along the way... is this not correct?
Answer by v-ercart · Nov 15, 2018 at 10:46 PM
I bet the value of hitPoint.position is wrong. You can test this by commenting out the contents of your Update() function, and you'll probably see it sitting at 0,0,0 not moving. Part of the reason I think this is true is your transform.position line doesn't edit the x and z values, so the transform must be set to those values initially.
Try printing hitPoint.position to the debug log when you get hit.
Your answer
Follow this Question
Related Questions
Does using multiple World Space Canvases affect performance? 0 Answers
Transform Position Are Different Each Other 1 Answer
UI Canvas Anchor points are stuck in the bottom left corner and disabled when in overlay mode. 1 Answer
Assign main camera to a prefab with a canvas 0 Answers
Saving canvas to prefab 0 Answers