Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8880731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:12:55+00:00 2026-06-14T20:12:55+00:00

I’m trying to convert (and understand) this simplified piece of code from unityscript to

  • 0

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
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:12:56+00:00Added an answer on June 14, 2026 at 8:12 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.