downdf
downdf

Reputation: 23

Unity C# Array type error (when converting from unityscript)

I'm trying to convert (and understand) this simplified piece of code from unityscript to C#:

private var actionList = new Array();                                                           

function Start()
{
actionList = new Array(12);

for(i=0; i<actionList.length; i++) 
    actionList[i] = new Array(4);
for(i=0; i<actionList.length; i++)
{
    actionList[i][1] = Rect(50+(i*55),50, 50, 50);        
    actionList[i][2] = new Array("action");     
}
actionList[0][3] = KeyCode.Alpha1;
actionList[1][3] = KeyCode.Alpha2;
actionList[2][3] = KeyCode.Alpha3;
actionList[3][3] = KeyCode.Alpha4;
actionList[4][3] = KeyCode.Alpha5;
actionList[5][3] = KeyCode.Alpha6;
actionList[6][3] = KeyCode.Alpha7;
actionList[7][3] = KeyCode.Alpha8;
actionList[8][3] = KeyCode.Alpha9;
actionList[9][3] = KeyCode.Alpha0;
actionList[10][3] = KeyCode.Minus;
actionList[11][3] = KeyCode.Equals;
}

function OnGUI()
{
for(i=0; i<actionList.length; i++)
    {
    if(actionList[i][1] != null)
        drawButton(actionList[i][1]);
    }
}

function drawButton(rect)
{
    GUI.Button(rect, "Hello");
}

The full code can be found here: Creating a Drag and Drop Spell Bar

My shot at this in C# (leaving out the keymappings):

using UnityEngine;
using System.Collections.Generic;

public class SpellBar : MonoBehaviour
{
private object[][] actionList;

void Start()
{
actionList = new object[12][];

for (int i=0; i<actionList.Length; i++)
    actionList[i] = new object[4];
for (int i=0; i<actionList.Length; i++)
{
    actionList[i][1] = new Rect(50+(i*55), 50, 50, 50);
}

//Keymappings
}

void OnGUI() 
{
for(int i=0; i<actionList.Length; i++)
    {
    if(actionList[i][1] != null)
        DrawButton(actionList[i][1]);
    }
}

void DrawButton(Rect rect)
{
    GUI.Button(rect,"Hello");
}
}

The error I get here is in OnGui, when using DrawButton(actionListi). Error: cannot convert 'object' expression to type "UnityEngine,Rect'.

I am not an experienced programmer in either unityscript or C#. I searched google for hours, trying Lists, ArrayLists etc, but cant find the solution. Thnx in advance!

EDIT 27-11-2012
So, I ran into a new related problem. Don't know if I should edit this in here, but I couldn't format properly in a reply.
In the JavaScript code I'm trying to convert, the following code is used to store a class in an array:

var openSlot = getNextSlot(actionList);    
if(openSlot != -1)
    actionList[openSlot][0] = new classEvilSmile();

While this is not exactly the way I'd want it, I would like to be able to store a method in an array, but I can't find a way to do it in C#. Is it simply impossible?
(Example) I'm trying to do something like this:

//In addition to what I posted above
void Start()
{
actionList[i][0] = void FireBall();
}

void FireBall()
{
Texture2D icon = Resources.Load("icon_fireball") as Texture2D;
String description = new String("Shoot a ball of fire");

//etc
}

Upvotes: 2

Views: 1024

Answers (1)

LightStriker
LightStriker

Reputation: 21024

Unlike Javascript, C# is strong-typed, which mean you cannot pass an object to a method that ask for a Rectangle. You must give the method that exact type, or a child derived from that type.

So, instead of

private object[][] actionList;

you should have

private Rectangle[][] actionList;

Strong-type language performs a lot of compile time validation to see if you don't pass the wrong variable type to the wrong method parameters. The advantage is two fold, first you don't have to perform test at runtime to know if the variable is of the correct type. Second, it gives an easy way to find errors in your code without the need to run every part of it. If it compiles, you're already 80% done.

However, if you want to combine different type within that array, you have multiple choices. You can use a class like Tuple<> to combine a set of object that doesn't have matching type or you can cast your object to Rectangle before feeding it to the method like:

DrawButton((Rectangle)actionList[i][1]);

However, if the cast is invalid, you will get a runtime assert.

Upvotes: 1

Related Questions