Hello
Im new to unity, and in never worked with js. There was a pretty good tutorial about a PickupSpawner which i wanted to use…
But i needed the code in c# because the rest of my project is in c# and i’m not used to js.
so here is the original js code:
#pragma strict
// minimum and maximum spawn delay times
public var minimumSpawnDelayTime:int = 1;
public var maximumSpawnDelayTime:int = 5;
// the pickup prefab, assigned via the Inspector
public var pickupPrefab:GameObject;
public var pickupPrefab2:GameObject;
public var pickupPrefab3:GameObject;
public var pickupPrefab4:GameObject;
// the number of pickups to have
public var numberOfPickups:int = 2;
// the ARRAY of spawnpoints that our pickup will be spawned at
private var spawnPointList:GameObject[];
// array of which spawn points are available
private var spawnIndexAvailableList:Array = [];
// variable to hold the total number of spawn points, saves having to recalculate
private var numberOfSpawnPoints:int;
public var destroyed = false;
function 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 (var i:int = 0; i < numberOfSpawnPoints; i++)
{
spawnIndexAvailableList *= true;*
-
}*
-
// spawn X amount of pickups according to numberOfPickups*
-
for (var j:int = 0; j < numberOfPickups; j++)*
-
{*
-
SpawnPickup();*
-
}*
}
function SpawnPickup()
{
-
// generate a random integer to use as the index to select a spawn point from the list*
-
var randomSpawnIndex:int = 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*
-
var spawnedPickupPosition:Vector3 = spawnPointList[randomSpawnIndex].transform.position;*
-
//var spawnedPickupRotation:Quaternion = spawnPointList[randomSpawnIndex].transform.rotation;*
-
// instantiate (create) the pickup prefab with the above position and rotation*
-
var x = Random.Range(1,10);*
-
//var spawnedPickup:GameObject = Instantiate(pickupPrefab, spawnedPickupPosition, spawnedPickupRotation);*
-
var spawnedPickup:GameObject;*
-
if (x <= 5)*
-
{*
-
spawnedPickup = Instantiate(pickupPrefab, spawnedPickupPosition, Quaternion.identity);*
-
}*
-
else*
-
{*
-
spawnedPickup = Instantiate(pickupPrefab2, spawnedPickupPosition, Quaternion.identity);*
-
}*
-
// 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*
-
spawnedPickup.name = randomSpawnIndex.ToString();*
-
// make the spawn index unavailable to prevent another pickup being spawned in this position*
-
spawnIndexAvailableList[randomSpawnIndex] = false;*
}
This is what i converted it to:
c#:
using UnityEngine;
using System.Collections;
public class PickupSpawner : MonoBehaviour {
// minimum and maximum spawn delay times
public int minimumSpawnDelayTime = 1;
public int maximumSpawnDelayTime = 5;
// the pickup prefab, assigned via the Inspector
public GameObject pickupPrefab1;
public GameObject pickupPrefab2;
public GameObject pickupPrefab3;
public GameObject pickupPrefab4;
// the number of pickups to have
public int numberOfPickups = 2;
// the ARRAY of spawnpoints that our pickup will be spawned at
private GameObject[] spawnPointList;
// array of which spawn points are available
private bool[] spawnIndexAvailableList;
// variable to hold the total number of spawn points, saves having to recalculate
private int numberOfSpawnPoints;
-
// Use this for initialization*
-
void Start () {*
-
// 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();*
* }*
* }*
* // Update is called once per frame*
* void Update () {*
* }*
* 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;*
* // instantiate (create) the pickup prefab with the above position and rotation*
* var x = Random.Range(1,10);*
* //var spawnedPickup:GameObject = Instantiate(pickupPrefab, spawnedPickupPosition, spawnedPickupRotation);*
* GameObject spawnedPickup;*
* if (x <= 5)*
* {*
* spawnedPickup = Instantiate(pickupPrefab1, spawnedPickupPosition, Quaternion.identity) as GameObject;*
* }*
* else*
* {*
* spawnedPickup = Instantiate(pickupPrefab2, spawnedPickupPosition, Quaternion.identity) 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*
* spawnedPickup.name = randomSpawnIndex.ToString();*
* // make the spawn index unavailable to prevent another pickup being spawned in this position*
* spawnIndexAvailableList[randomSpawnIndex] = false;*
}
There are no compiling errors, but still it doesnt work. I assigned everything as it was done in the tutorial…
There is one warning in line 23:
private bool[] spawnIndexAvailableList;
Field PickupSpawner.spawnIndexAvailabeList is never assigned to, and will always it default value ‘null’
What does it mean exactly?
Anyone any ideas?
greetings