I have a question about adding “Heart” Health prefab to a panel inside of a canvas so that when I switch between Mobile and WebPlayer…it resizes perfectly. I currently have a hierarchy that looks like this…
-Canvas
-Panel
-hearts (empty gameObject with a script on it)
I have two other items on the same Panel(in a different project, I’m just trying to get this to work) and they scale Perfectly but they are just UI Text items. The Hearts will be added at the start of the game and how many is decided before start up. Currently, it works but doesn’t scale well with the Panel. I would like to attach them to the panel and it scale with the panel and I’m not sure if I’m even headed in the right direction with it…here is a few bits of code that is what I’m working with
script1;
public class heartAdder : MonoBehaviour {
public GameObject heartGUI;
public Vector2 startPoint = new Vector2(0.05f,0.95f);
public float distance = 0.04f;
GameObject P;
private float x;
private float y;
private float z;
void addHearts (int amount){
P = GameObject.Find ("hearts");
RectTransform RT = (RectTransform)P.transform;
x = RT.localPosition.x;
y = RT.localPosition.y;
z = RT.localPosition.z;
for(int i = 0;i < amount;i++){
Vector3 pos = new Vector3(startPoint.x+(distance*i), startPoint.y,0);
//Vector3 pos = new Vector3(x+(distance*i), y,0);
GameObject heartPrefab = Instantiate(heartGUI, pos, Quaternion.Euler(0,0,0)) as GameObject;
heartPrefab.transform.name = "heart"+(i+1).ToString();
heartPrefab.transform.parent = transform;
}
}
}
script 2; that calls the heart adder
![void Start () {
health = hearts*2;
//GameObject getHearts = GameObject.Find("GUI/hearts");
GameObject getHearts = GameObject.Find ("Panel/hearts");
getHearts.SendMessage("addHearts", hearts, SendMessageOptions.DontRequireReceiver);
GUITexture[] allChildren = getHearts.GetComponentsInChildren<GUITexture>();
heartsGUI = new GUITexture[allChildren.Length];
health = allChildren.Length*2;
for(int i = 0;i < allChildren.Length;i++){
heartsGUI _= allChildren *as GUITexture;*_
* }*
* rend = GetComponent();*
* }][1]*
_*
hopefully there is enough information here to get what I’m asking,
Thanks in advance for any hints/tips/guidance!!!