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
}