- Home /
js conversion to c#. no errors but still not working
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[i] = 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[i] = 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
Other than @Chronos-L's catch the only other difference I see is the use of Start() rather than Awake() (which should not be a functional difference). Code like this is ideal to debug in the monodevelop debugger since it only runs once and the indices of the loops are small:
Thanks you two
Now
I debugged it just in that way and everything works fine until here: for (int i = 0; i < numberOfSpawnPoints; i++) { spawnIndexAvailableList[i] = true; }
each value in the spawnIndexAvailableList array is succesfully set to true, but then when it hits the end of the array it crashes...
any idea?
Answer by Chronos-L · Jun 10, 2013 at 02:10 PM
"There are no compiling errors", but I think the C# version of the code might produce a runtime error since you do not initialize the spawnIndexAvailableList
.
Before Line 40, add this line spawnIndexAvailableList = new bool[numberOfSpawnPoints];
, I think that is the only thing I can add to the C# code. Other than that, I do not see any difference between the JS and C#.
Your answer
![](https://koobas.hobune.stream/wayback/20220613103910im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Some Basic Conversions from JavaScript to C# 1 Answer
Integer to Character in unityscript 2 Answers
Converting a JS script into C# - hit a wall with arrays... 1 Answer
Help with conversion from javascript to c# 3 Answers
Multiple Cars not working 1 Answer