How to randomly generate pickups

This is driving me crazy. I’m trying to convert UnityScript to C# for a tutorial I’m working on. I got pretty far until I had to do arrays - something I haven’t been good at since I started programming. I keep getting an error and don’t know how to fix it. Here’s my code:

 using UnityEngine;
    using System.Collections;

public class PickupController : MonoBehaviour 
	//the pickup prefab assigned via the Inspector
	public GameObject pickupPrefab;
	private GameObject spawnedPickup;
	//the number of pickups to have around the level at any one time
	public int numberOfPickups = 2;
	// the ARRAY of spawnpoints that our pickup will be spawned at
	private GameObject[] spawnPointList;
	// array of which spawn points are currently available for spawning at
	private ArrayList spawnIndexAvailableList;
	// variable to hold the total number of spawn points, saves having to recalculate
	private int numberOfSpawnPoints;

	void Awake()
		// retrieve GameObjects tagged as 'SpawnPoint' within the 'PickupSpawnPoints' GameObject which this script is a Component of
		spawnPointList = GameObject.FindGameObjectsWithTag("SpawnPoint");
		// retreive number of spawn points
		numberOfSpawnPoints = spawnPointList.length;
		// make sure number of pickups doesn't exceed number of spawn points
		if (numberOfPickups > numberOfSpawnPoints) numberOfPickups = numberOfSpawnPoints;
		// make all spawn points available by setting each index to true
		for (int i = 0;  i < numberOfSpawnPoints; i++) 
				spawnIndexAvailableList *= true;*
  •  	}*
  •  // spawn X amount of pickups according to numberOfPickups*
  •  for (int j = 0;  j < numberOfPickups; j++)* 
  •  	{*
  •  		SpawnPickup();*
  •  	}*
  • }*

  • void SpawnPickup()*

  • {*

  •  // generate a random integer to use as the index to select a spawn point from the list*
  •  int randomSpawnIndex = Random.Range(0, numberOfSpawnPoints);*
  •  // while the selected spawn index is unavailable regenerate another one*
  •  while (!spawnIndexAvailableList[randomSpawnIndex])*
  •  	{*
  •  		randomSpawnIndex = Random.Range(0, numberOfSpawnPoints);*
  •  	}*
  •  // retrieve the position and rotation of the pickups spawn point*
  •  Vector3 spawnedPickupPosition = spawnPointList[randomSpawnIndex].transform.position;*
  •  Quaternion spawnedPickupRotation = spawnPointList[randomSpawnIndex].transform.rotation;*
  •  // instantiate (create) the pickup prefab with the above position and rotation*
  •  spawnedPickup = Instantiate(pickupPrefab, spawnedPickupPosition, spawnedPickupRotation)as GameObject;*
  •  // set the spawned pickup as a child of the 'PickupSpawnPoints' gameobject that this script is a Component of*
  •  // this is so we can use SendMessageUpwards within scripts attached to the pickupPrefab to call functions within this script*
  •  spawnedPickup.transform.parent = spawnPointList[randomSpawnIndex].transform;*
  •  // set the name of the pickup as its index*
  • = randomSpawnIndex.ToString();*
  •  // make the spawn index unavailable to prevent another pickup being spawned in this position*
  •  spawnIndexAvailableList[randomSpawnIndex] = false;*
  • }*

This is what I have up to this point in the tutorial. Hopefully once I get the arrays fixed I can get the rest of the tutorial done tonight. Any suggestions?

private ArrayList spawnIndexAvailableList;

You need to initialize the ArrayList before you can use it. Everything that is not a native type (int, float, bool, etc) must be constructed in C# (technically I think native types are as well, but the compiler handles that for you…I think):

private ArrayList spawnIndexAvailableList = new ArrayList();

ArrayList also needs to have items added to it using spawnIndexAvailableList.Add() if you construct it empty. If you know the size of the ArrayList ahead of time, you can create it as follows (I’m trying to tie it into your code):

ArrayList spawnIndexAvailableList = new ArrayList(numberOfSpawnPoints);

This code line would go in your Awake() method right after you set numberOfSpawnPoints.

With an ArrayList, it only knows that it’s holding objects but doesn’t know exactly what it’s holding. If your spawnIndexAvailableList will only ever contain a list of boolean values, try explicitly casting to a bool in your while loop. Something like this:

 // while the selected spawn index is unavailable regenerate another one
while (!(bool)spawnIndexAvailableList[randomSpawnIndex])
randomSpawnIndex = Random.Range(0, numberOfSpawnPoints);

Check out this topic, maybe it will be helpful.