Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Captain_Dando · Feb 21, 2013 at 03:41 PM · c#arrays

How can I create an array of objects in C#?

Hi there. I am moving over from javascript to C# and am having difficulty understanding some of the same concepts I used in javascript. for example, In javascript I have created this code;

 var array = new Array ();
 
 array[0] = { "x": 1,
              "y": 10,
              "dialogue": "stuff",
              "show": true
             }
 
 array[2] = { "x": 1,
              "y": 10,
              "dialogue": "stuff",
              "show": false
             }
 
 array[10] = {"x": 1,
                 "y": 10,
              "dialogue": "more stuff",
              "show": true
             }

as you can see, I have put an object into the indexes of 0, 2, and 10. from what I understand, you can only have variables of one type in an array in C#. could anyone show me how I could create this code in C#?

on a side note, in this code in javascipt I would iterate through the array searching for indexes which contained information by using the conditional

 if (array[i] != undefined)

could this work in C#?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SubatomicHero · Feb 21, 2013 at 06:27 PM 2
Share

What was going into that array in JS?

It seems from your code you were using the equivalent of an Arraylist.

But here is an example of creating an array of integers in C#:

 public int[] intarray = new intarray[*size goes here*];

Here is a good link for understanding arrays in C#: C# Arrays

Anyways, good luck

avatar image Captain_Dando · Feb 22, 2013 at 07:26 AM 1
Share

$$anonymous$$y experience with objects has mainly been with json calls in sites that use ajax calls. In this case, I would be able to access the x, y, dialogue and show properties of entries in an array. I'm using them to contain the properties of 10 separate labels in a scene, who's contents could be different at different times.

For example, I could store a piece of dialogue in two different labels that render simultaneously, and still be able to change their properties individually later on. When OnGUI runs, it loops through each entry in the array and sets it's size to x and y (though I have more properties in my script) and the contents of the box to dialogue.

The problem is, it needs to be an object, and from what I've seen C# only supports integers, floats and strings

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by paulaceccon · Feb 21, 2013 at 06:09 PM

It seems that you could create a class like:

 public class Item
 {
     int x;
     int y;
     string dialogue;
     bool show;
 
     public Robot( int _x, int _y, string _dialogue, bool _show )
     {
         x = _x;
         y = _y;
         dialogue = _dialogue;
         show = _show;
     }
 }

Then, you could create getters to access these variables.

So, you could create a list like this:

 public class myClass: MonoBehaviour
 {
     public List<Item> items;
 
         void Start (){
             items = new List</item>();
             items.Add (new Item (1, 10, "stuff", true));
             items.Add (new Item (1, 10, "stuff", false));
         }
 }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Captain_Dando · Feb 22, 2013 at 10:23 AM 0
Share

it says that robot needs a return type, but I'm not sure what it is

avatar image paulaceccon · Feb 22, 2013 at 11:52 AM 0
Share

In which line?

avatar image paulaceccon · Feb 22, 2013 at 12:01 PM 0
Share

This was usefull? If so, could you accept it or voted it up? Thks.

avatar image Bunny83 · Feb 22, 2013 at 12:37 PM 1
Share

It seems "Robot" should be the constructor of the class, but you called your class "Item". So either change the class name to "Robot" or the constructor should be called "Item"

avatar image PAHeartBeat · Feb 22, 2013 at 01:01 PM 1
Share

it should be like that

 public Item ( int _x, int _y, string _dialogue, bool _show )
 {
     x = _x;
     y = _y;
     dialogue = _dialogue;
     show = _show;
 }


 public Item (type parameterName){
 //your code goes here
 }

is by default return it's self as constructor overload

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Array empties values on RunTime? 1 Answer

Array with two values per element 1 Answer

Algorithm for loading sounds 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges