- Home /
Convert Array
I'm trying to test this tutorial game on the adroid OS but the tutorial only seems to work on the PC/Web player builds. Now, I beleive my problem lies with my 3 arrays that I have in my script, because Android can't hand JS GUI arrays, and they need to be changed to C# arrays.. (if im understanding it right) SO my question is, how do change them? Im new to scripting and this is a little beyond me right now. I get errors when I try to compile for Android.. they say BCE0019 and BCE0048 ('item' not a memeber of object and object does not support slicing) Please help! Here is my script..
void OnGUI (){
GUILayout.BeginArea ( new Rect(0,0,Screen.width,Screen.height));
BuildGrid();
if(playerHasWon) BuildWinPrompt();
GUILayout.EndArea();
print("building grid!");
}
int cols = 4;
int rows = 4;
int totalCards = cols*rows;
int matchesNeededToWin = totalCards * 0.50f;
int matchesMade = 0;
int cardW = 100;
int cardH = 100;
Array aCards;
Array aGrid;
ArrayList aCardsFlipped = new ArrayList();
bool playerCanClick;
bool playerHasWon = false;
void BuildGrid (){
GUILayout.BeginVertical();
GUILayout.FlexibleSpace();
for(i=0; i<rows; i++)
{
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
for(j=0; j<cols; j++)
{
Object card = aGrid[i] [j];
string img;
if(card.isMatched)
{
img = "blank";
}
else
if(card.isFaceUp)
{
img = card.img;
}
else
{
img = "wrench";
}
GUI.enabled = !card.isMatched;
if (GUILayout.Button(Resources.Load(img),
GUILayout.Height(cardH),GUILayout.Width(cardW)))
{
if(playerCanClick)
{
FlipCardFaceUp(card);
Debug.Log(card.img);
}
}
GUI.enabled = true;
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
}
GUILayout.FlexibleSpace();
GUILayout.EndVertical();
}
void Start (){
playerCanClick = true;
string[] aCards;
string[] aGrid;
aCardsFlipped = new ArrayList();
BuildDeck();
for(i=0; i<rows; i++)
{
aGrid[i] = new ArrayList();
for(int j=0; j<cols; j++)
{
int someNum = Random.Range(0,aCards.length);
aGrid[i] [j] = aCards[someNum];
aCards.RemoveAt(someNum);
}
}
}
class Card extends System.Object
{
bool isFaceUp = false;
bool isMatched = false;
string img;
int id;
void Card ( string img , int id ){
this.img = img;
this.id = id;
}
}
void BuildDeck (){
int totalRobots = 4;
Card card;
int id = 0;
for(i=0; i<totalRobots; i++)
{
aRobotParts : string [] = ["Head", "Arm", "Leg"];
for(j=0; j<2; j++)
{
int someNum = Random.Range(0, aRobotParts.length);
string theMissingPart = aRobotParts[someNum];
aRobotParts.RemoveAt(someNum);
card = new Card("robot" + (i+1) + "Missing" + theMissingPart, id);
aCards.Add(card);
card = new Card("robot" + (i+1) + theMissingPart,id);
aCards.Add(card);
id++;
}
}
}
void FlipCardFaceUp ( Card card ){
card.isFaceUp = true;
if(aCardsFlipped.IndexOf(card) < 0)
{
aCardsFlipped.Add(card);
if(aCardsFlipped.Count == 2)
{
playerCanClick = false;
yield return new WaitForSeconds(1);
if(aCardsFlipped[0].id == aCardsFlipped[1].id)
{
//MATCH!
aCardsFlipped[0].isMatched = true;
aCardsFlipped[1].isMatched = true;
matchesMade ++;
if(matchesMade >= matchesNeededToWin)
{
playerHasWon = true;
}
}
else
{
aCardsFlipped[0].isFaceUp = false;
aCardsFlipped[1].isFaceUp = false;
}
aCardsFlipped = new ArrayList();
playerCanClick = true;
}
}
}
void BuildWinPrompt (){
int winPromptW = 100;
int winPromptH = 90;
float halfScreenW = Screen.width/2;
float halfScreenH = Screen.height/2;
int halfPromptW = winPromptW/2;
int halfPromptH = winPromptH/2;
GUI.BeginGroup( new Rect(halfScreenW-halfPromptW,halfScreenH-halfPromptH,
winPromptW, winPromptH));
GUI.Box ( new Rect(0,0,winPromptW,winPromptH),
"YOU WIN!");
if(GUI.Button( new Rect(10,40,80,20), "Play Again?"))
{
Application.LoadLevel("Title");
Answer by Joshua · Sep 01, 2011 at 05:24 PM
You're absolutely right that you cannot use the 'Array' class, which is JS only - and also extremely slow so should be avoided even there if possible.
Instead you'll need to use normal, build-in, arrays. Have a look here, figure out the differences and change it - it's not that difficult but it's important you understand how they work.
Ok, so as I said.. extremely new to this, like my first week.. so would it be like this? original var aRobotParts:Array = ["Head", "Arm", "Leg"];
changed Array aRobotParts = ["Head", "Arm", "Leg"];
but I need to change the variables associated with the array too right? format them different? I'm still kind of lost with the formatting.. I checked out that site...it is helpful. Thankyou.
At the top, you have:
var aCards:Array;
this is a javascript array, which are a huge pain because they are dynamically typed - so you don't know array of what type they are, or if they even contain only a single type.
from the context is seems aCards would be an array of strings, which would then be:
var aCards : string[]; //js
string[] aCards; //c#
So, that means
aRobot : Array = ["Head", "Arm", "Leg"];
becomes
aRobot : string[] = ["Head", "Arm", "Leg"];
ok, So i read that website a bunch, and researched some other sites, and tried to fix my code. Now I'm getting syntax errors at lines 2 and 3 for missing ; but theres already one there.. or it doesnt need one.. so now what am I doing wrong?? Is this script right with the arrays?? I tried to make it more in the c# then js.. But I don't know if I did it right. Reposting script in my original question..
that's strange. I don't see anything wrong with those lines of code. Have you tried restarting Unity (sometimes it helps...) If it's still complaining afterwards, please post the error-message. is it uce0001 or a cs-something?
Hey again, well heres the errors:
Assets/GameScript.js(2,5): UCE0001: ';' expected. Insert a semicolon at the end. Assets/GameScript.js(2,14): UCE0001: ';' expected. Insert a semicolon at the end. Assets/GameScript.js(3,80): BCE0044: expecting :, found ';'.
Answer by Sigil · Sep 03, 2011 at 07:30 AM
I'm a little confused by where the comments have gone with this one. So JavaScript Arrays aren't allowed for Android, isn't it possible to use ArrayList instead of converting everything over to C#?
I've looked around a bit, and I don't see a clear answer on this one, but it seems that the JavaScript Array class is the only special thing here, and all bloodchylde needs to do is convert those to ArrayLists instead, not even explicit arrays or anything.
Am I missing something?
Your answer
![](https://koobas.hobune.stream/wayback/20220612195812im_/https://answers.unity.com/themes/thub/images/avi.jpg)