- Home /
Unity 5 - UI Text
Hi guys! I hope you can help me with a simple problem. I'm trying to simulate a "damage score" text on top of the player's head when it gets hit by an object.
But the problem is I can't make the position when instantiating it right on top of the player.
This is my code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class Rotator : MonoBehaviour {
public GameObject damage;
//button clicked
public void onDamage(){
GameObject d = (GameObject) Instantiate(damage, transform.position, Quaternion.identity);
}
}
Any advice guys?
Answer by Hellium · Jul 13, 2015 at 08:14 AM
You problem is not clear ...
The position isn't correct, right ?
You should add an empty point as a child of your player as an anchor point. Add a public Transform reference at the top of the script and drag & drop the anchor point from the inspector.
Then, you do as follow :
GameObject d = (GameObject) Instantiate(damage, transform.position, Quaternion.identity);
d.SetParent(anchorPoint);
d.transform.localPosition = Vector3.zero;
Just tried that and didn't work. I think it's probably because it's using Rect Transform ins$$anonymous$$d of just Transform component.
Well, you didn't mention that ! ;)
Then the solution is slightly different but needs the same anchor position :
Vector3 UIposition = Camera.main.WorldToViewportPoint(anchorPoint.position ) ;
d.GetComponent<RectTransform>().anchor$$anonymous$$in = UIposition ;
d.GetComponent<RectTransform>().anchor$$anonymous$$ax = UIposition ;
Your answer
Follow this Question
Related Questions
How can i pay a animation again and agan? 1 Answer
Animating Transitions for GUI Results 0 Answers
Can the animation editor create local rotational data? 3 Answers
Adding animation clips via script 2 Answers