Quantcast
Viewing all articles
Browse latest Browse all 131

Array of Objects of a Class

I would assume this is relatively straightforward. I just switched over from UnityScript to C#, and I have to admit it is largely superior, even in the fact that it very tangibly helps you understand the object-class relationships in the game project. I all ready prefer it. Anyways, I'm just trying to get a quick array set up of references to instances of a SpawnPoint class. SpawnPoint is a script attached to a Spawn Point prefab, and I'm just cycling through them when a level loads to get a list of them. However, I am having trouble populating this array. To get a reference to the class, I am using GetComponent. I understand why this doesn't work- GetComponent returns a type and not a variable/value. I just don't know what other method I should employ here. Thanks for the help! - YA P.S: Is there any way for me to get rid of the automagically created 'Use this for initialization' and 'Updates every frame' comments? They're a small annoyance but still... using UnityEngine; using System.Collections; public class SpawnCont : MonoBehaviour { public SpawnPoint[] pointArr; public GameObject player; public SpawnPoint current; // Use this for initialization void Start () { pointArr = new SpawnPoint[ transform.childCount ]; //Because of this, don't make anything but spawnpoints children int count = 0; //Make sure we get through full list foreach( Transform child in transform ){ //The important part //THIS IS WHERE THE ERROR IS pointArr[count] = child.GetComponent( SpawnPoint ); count += 1; } SetActive ( pointArr[0] ); Debug.Log ( "Spawning Player at: " + pointArr[0].pos.x ); Spawn( player ); } void SetActive( SpawnPoint myPoint ){ for( int i = 0; i < pointArr.Length; i++ ){ pointArr[i].activated = false; } myPoint.activated = true; current = myPoint; } void Spawn( GameObject player ){ Instantiate( player, current.pos, Quaternion.identity ); } }

Viewing all articles
Browse latest Browse all 131

Trending Articles