- Home /
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 );
}
}
Answer by iamvishnusankar · Feb 11, 2014 at 06:11 PM
1.If you're trying to get the SpawinPoint from SpawnPointScript
2.First define a spawn point return function on your SpawnPoint script.
2.1.For example let the function be "ReturnSpawnPoint()"
And at the start() of SpawnCount script use the following code:
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
//this will create an instance
current = child.GetComponent<SpawnPoint>();
foreach( Transform child in transform ){
//The important part
//This will assign the spawnpoint
pointArr[count] = current.ReturnSpawnPoint();
count += 1;
}
SetActive ( pointArr[0] );
Debug.Log ( "Spawning Player at: " + pointArr[0].pos.x );
Spawn( player );
}
$$anonymous$$y Return function was a very simple:
public SpawnPoint ReturnSpawnPoint(){
return this;
}
And your solution worked well. However, is there any other way? I would hate to waste my time writing otherwise useless self-return functions every time I want a reference to an instance of a class on an Object. I refuse to believe there are no other solutions.
Thanks for the help! - YA
Yes, Ins$$anonymous$$d of writing repeated self returning functions on every object you want to get spawned or something like that ;
Just make one "Empty GameObject" and and attach all your "$$anonymous$$aster Scripts". Use that Object as the reference for all other objects. Thereby you could solve the problem of repeat blocks of code.
Haha that is even more impractical than before. This is fine it will work for now. Thank you once again! -YA