Spawn Points as GameObjects - Receiving NullReferenceException - Need help please!

i know what my issue is but I’ve no idea how to fix it. I have an Game Object with 50 Spawn Points as children, is there a way of using a for loop to assign each of the spawn points to an array. The spawn points were created inside unity and not via scripts. i have it like this as i would like to use the game objects position in the world to spawn a light bulb in its place. Between 10-15 light bulbs will be spawned in the game, which is my reasoning for having 50 possible locations to put the light bulbs.

public class LightBulbSpawn : MonoBehaviour {
    public GameObject lightBulb;
    public GameObject[] spawnPoints;
    private int NumOfBulbs;


    // Use this for initialization
    void Start () {
        PickPositions ();
    }
    void PickPositions(){
        spawnPoints = new GameObject[50];
        NumOfBulbs = Random.Range (10, 15);
        GameObject tiornan;
        for (int i = 0; i <= NumOfBulbs; i++) {
            for (int x = 0; x < NumOfBulbs; x++) {
                int temp = Random.Range (0, 49);
                tiornan = Instantiate (lightBulb, spawnPoints [temp].transform.position, Quaternion.identity)as GameObject;
            }

        }


    }

Since you spawnPoints array is public, you can just select all your spawn point objects and drag and drop them onto the array in the inspector. It will add them all as a group (so you don’t have to do each one one at a time)

You can always set a transform root for your spawn points and just add them as a children. That way you can freely iterate over that SpawnPoint root transform and pick the one you want. No dragging/dropping required except for the spawn root transform itself.

Also, you don’t need to cast results of instantiate to a GameObject anymore.

when i do this, every time i play test the array is emptied and all of the spawns are gone. Yet when i stop testing the spawns return into the array.

You’re clearing array each time you start:
@

spawnPoints = new GameObject[50];

Don’t do it.

Yep, what he said. This is also why you get a null error, because you’re creating a large array, but not filling it.