Question by
RensDevolp · Dec 03, 2016 at 12:15 PM ·
guiarraystringcomponent
Access the variables of components array?
Hi all,
This question is just driving me crazy! How do I access the variables of another script via an array? I have the following scripts: (this one is with the error) using UnityEngine; using System.Collections;
public class SoldierManager : MonoBehaviour {
public Transform[] Soldiers;
Transform SH;
PlayerID[] playerID;
HealthManager[] healtManager;
// Use this for initialization
void Start () {
SH = transform.FindChild ("SoldierHolder");
Soldiers = SH.gameObject.GetComponentsInChildren<Transform> ();
playerID = new PlayerID[Soldiers.Length];
for (int i = 0; i < Soldiers.Length; i++) {
playerID [i] = Soldiers [i].GetComponent<PlayerID> ();
}
}
// Update is called once per frame
void Update () {
}
void OnGUI(){
for( int i = 0; i < playerID.Length; i++ ) {
GUILayout.Label (playerID.PlayerName);
}
}
}
and this is the id script:
using UnityEngine;
using System.Collections;
public class PlayerID : MonoBehaviour {
public int playerID;
public string playerName;
public enum Rank{PVT, PV2, PFC, SPC, CPL, SGT, SSG, SFC, MSG, FSG, SGM, CSM, SMA};
public Rank rank;
}
Comment
Best Answer
Answer by RensDevolp · Dec 03, 2016 at 12:33 PM
Oh i figured it out by myself. I had to do "foreach" instead of "for".
Thanks anyway
Your answer
