- Home /
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!
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();
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.
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
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?
It looks like removing the brackets did the trick! Thanks!