- Home /
Fixed
Converting from js to c#
Hi, I have an old script that I want to convert to c#, but I'm getting errors, you can find the original script here: http://answers.unity3d.com/questions/423663/select-randomly-x-arrays-and-instantiate-them.html
I tried converting, this is what I have done so far, and now I don't know how to fix the errors it has.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BrickGenerator : MonoBehaviour {
public int bricksToSpawn = 85;
public GameObject[] bricks;
List<GameObject> bricksListmeObject = new List<GameObject>(); // declare a new list
void Start() {
PopulateBricksList();
ChooseRandomBricks();
}
void PopulateBricksList() {
// declare a new list
List<GameObject> bricksList = new List<GameObject>();
// get the length of the built-in array
int totalBricks = bricks.Length;
// add each brick to the brickList
for (int i = 0; i < totalBricks; i++){
bricksList.Add(bricks[i]);
}
}
void ChooseRandomBricks() {
// choose a brick from the bricksList
for (int i = 0; i < bricksToSpawn; i++ ){
// find the current length of the bricksList
int bricksRemaining = bricksList.Count;
// get a random number
int rndChoice = Random.Range(0, bricksRemaining - 1);
// instantiate that chosen brick
Instantiate(bricksList[rndChoice], brickList[rndChoice].transform.position, transform.rotation);
// remove that brick from the list
bricksList.RemoveAt(rndChoice);
}
}
}
Here are the erros: https://www.dropbox.com/s/kboizlpgu3gas5v/Captura%20de%20tela%202014-04-06%2016.21.50.png
Thanks in advance.
Answer by getyour411 · Apr 06, 2014 at 03:31 PM
You declared bricksList inside PopulateBricks(), so it can only be called within that context; you could move it to the top/generic variable declare area.
I think the script had an useless variable, I maybe fixed it now, I'll test it
Follow this Question
Related Questions
Create a guitexture When clicked on 3d text? 0 Answers
js to C# converstion Problem 3 Answers
Convert js to C# Serializer problem 1 Answer
.js to C# conversion 4 Answers