I have a unit prefab which has a canvas as a child. The canvas contains an image used as a health bar. The position of the canvas on the prefab is something like (0, 2.8, 0), and stays in position for units that I played in the scene BEFORE runtime, yet when I instantiate a new unit from the exact same prefab, the canvas is at (0,0,0). Why?
You got to set the slider anchors around the health bar slider, assuming you’ve used slider for your health bar. Setting the anchors in a correct manner will help you get rid of this issue.
shows you how to create a Health bar using slider and set the anchor points of the slider.
Hope this help!!
I had this same issue, like AleKahpwn said I have resorted to setting the Canvas component position via code:
var canvas = gameObject.FindInChildren("Canvas");
var canvasePos = canvas.GetComponent<RectTransform>();
canvasePos.localPosition = new Vector3(0,8,0);
var objectName = gameObject.FindInChildren("PlayerNameText");
var playerName = objectName.GetComponent<Text>();
playerName.text = "FINALLY";
public static GameObject FindInChildren(this GameObject go, string name)
{
return (from x in go.GetComponentsInChildren<Transform>()
where x.gameObject.name == name
select x.gameObject).First();
}