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 TRON-dll · Dec 11, 2014 at 11:49 AM · c#arrayobjectclassclasses

Instantiating objects from a class? (C#)

Sorry if this is kind of a general C# question, but I've been having a lot of trouble with this, and I've only done class declarations and instantiation in C++, not C# or Unity, for that matter.

Long story short: I need to have a two-dimensional array of two dimensional integer arrays, and I've decided that the best way of accomplishing this is by creating a class that contains a two-dimensional integer array, and then having an array of objects of that class.

The problem is that I'm having a lot of trouble figuring out how to create my class and how to instantiate objects from it. If it helps, here's what the code for my class, which we'll call MyClass, looks like:

 using System.Collections;
 namespace ClassOfMine
 {
     public class MyClass
     {
         int[,] contents = new int[10,10];
         
         public MyClass ()
         {
             contents = new int[10,10]{
                 {0,0,0,0,0,0,0,0,0,0},
                 {0,0,0,0,0,0,0,0,0,0},
                 {0,0,0,0,0,0,0,0,0,0},
                 {0,0,0,0,0,0,0,0,0,0},
                 {0,0,0,0,0,0,0,0,0,0},
                 {0,0,0,0,0,0,0,0,0,0},
                 {0,0,0,0,0,0,0,0,0,0},
                 {0,0,0,0,0,0,0,0,0,0},
                 {0,0,0,0,0,0,0,0,0,0},
                 {0,0,0,0,0,0,0,0,0,0},
             };
         }
         
         public int GetValue(int x, int y)
         {
             return contents[x,y];
         }
         
         public void SetValue(int x, int y, int n)
         {
             contents[x,y] = n;
         }
     }
 }

I just need a kick in the right direction or something, I guess. I'm not sure how to instantiate a MyClass object in my other scripts. Any help is appreciated! Thanks!

Comment
Add comment
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

1 Reply

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

Answer by Paulius-Liekis · Dec 11, 2014 at 01:46 PM

 MyClass obj = new MyClass();

Is this what you're after?

2D:

 MyClass[,] obj = new MyClass[10,10];
 for (int i = 0; i < obj.GetLength(0); ++i)
     for (int j = 0; j < obj.GetLength(1); ++j)
         obj[i, j] = new MyClass();



Comment
Add comment · Show 4 · 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 TRON-dll · Dec 11, 2014 at 07:40 PM 0
Share

Hmm.. Odd. I thought that's what I had on my desktop last night, and it's working fine on my laptop. I may have missed a semicolon somewhere or something, haha. Just so I can make sure I'm doing everything right, here's my test driver file:

 using UnityEngine;
 using System.Collections;
 using ClassOf$$anonymous$$ine;
 
 public class OutputScript : $$anonymous$$onoBehaviour {
 
     // Use this for initialization
     void Start ()
     {
         $$anonymous$$yClass obj = new $$anonymous$$yClass();
         
         Debug.Log(obj.GetValue(0,0));
         
         obj.SetValue(0,0,1);
         
         Debug.Log(obj.GetValue(0,0));
     }
 }

This script does correctly output to the console a zero, followed by a 1.

avatar image TRON-dll · Dec 11, 2014 at 08:08 PM 0
Share

I tried doing this in my test driver script:

 using UnityEngine;
 using System.Collections;
 using ClassOf$$anonymous$$ine;
 
 public class OutputScript : $$anonymous$$onoBehaviour {
     
     // Use this for initialization
     void Start ()
     {
         $$anonymous$$yClass[,] obj = new $$anonymous$$yClass[10,10]();
         for (int i = 0; i < obj.GetLength(0); i++)
         {
             for (int j = 0; j < obj.GetLength(1); j++)
             {
                 obj[i,j] = new $$anonymous$$yClass();
             }
         }
         
         Debug.Log(obj.GetValue(0,0));
     }
 }

Now I'm getting the following error on the line where I define the $$anonymous$$yClass array:

error CS0119: Expression denotes a 'value', where a 'method group' was expected

avatar image Paulius-Liekis · Dec 11, 2014 at 08:34 PM 0
Share

Sorry, my bad. I think the problem is on this line:

 $$anonymous$$yClass[,] obj = new $$anonymous$$yClass[10,10]();

Remove the brackets:

 $$anonymous$$yClass[,] obj = new $$anonymous$$yClass[10,10];

If it's not that line, then tell us which line is it?

avatar image TRON-dll · Dec 11, 2014 at 09:28 PM 0
Share

It looks like removing the brackets did the trick! Thanks!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Need my function to work with different lists of different values (classes) 1 Answer

adding classes to arrays 2 Answers

Array of custom properties? (C#) 1 Answer

Can I choose what to pass into .getcomponent<{VARIABLE}>(); 1 Answer

Writing an class array to disk? 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