- Home /
Assign RectTransform to Array based on another Array
I'm using a script to create a UI based minimap and I'm trying to "extend" it further so that it can be used with more than one player in an efficient manner. This is what I have thus far:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class minimap : MonoBehaviour {
public GameObject player;
public RectTransform playerIcon;
public RectTransform iconCollection;
public RectTransform minimapBounds;
public Camera cam;
Vector2 ViewportPos;
Vector2 WorldScreen;
void Start ()
{
minimapBounds = minimapBounds.GetComponent<RectTransform> ();
cam = cam.GetComponent<Camera> ();
}
void Update ()
{
Vector2 ViewportPos = cam.WorldToViewportPoint (player.transform.position);
Vector2 WorldScreen = new Vector2 (((ViewportPos.x * minimapBounds.sizeDelta.x) - (minimapBounds.sizeDelta.x * 0.5f)), ((ViewportPos.y * minimapBounds.sizeDelta.y) - (minimapBounds.sizeDelta.y * 0.5f)));
playerIcon.anchoredPosition = WorldScreen;
}
}
I've tried using an array GameObject[] Player; along with an array for the newly instantiated RectTransforms called iconCollection then within the start function using a foreach loop as such:
void PlayerMarker()
{
foreach(GameObject obj in player)
{
RectTransform clone = Instantiate(playerIcon, transform.position, transform.rotation) as RectTransform;
iconCollection[] = clone;
}
}
I get an error with the iconCollection array so I'm not entirely sure how to assign these to this array. So I'm looking to instantiate all the player icons based on how many objects are in the "Player" array, then use those icons the same way I used a single icon in the update function when using just one player. Is there an easier way to do this? It seems like I'm nearly there but can't think this through as I don't use arrays much. Any help is greatly appreciated.