- Home /
adding classes to arrays
I've been working with arrays to get familiar with them and hopefully create an inventory system. I can't think of a way to create an class and add it to an array. I looked at a script online where the creator made a class in another script and then added it to an array in the main script. i really just want to add a set of information to an array; for example, an object/class with a name, description, and an associated game object. I know you can add game objects and such to arrays:
var object : Transform;
var arr = new Array();
function Start (){
arr.Add(object);
}
Something like that. does anyone have any idea how i could add a class to the array?
Answer by jahroy · Nov 21, 2011 at 02:31 AM
Here's some example code that uses a test class named FunkyItem.
Like you requested, a FunkyItem object has a name, a description, and a reference to a GameObject.
If you assign a GameObject to the slot named Test Object in the Inspector, the Start function will demonstrate the use of classes and constructors (when you press play).
I just typed this on the fly, so I'm sure there are a few syntax errors. Hope it helps!
/* a test object - set in the Inspector */
var testObject : GameObject;
/ how many items to put in the array when the Start function runs /
var arraySize : int = 10;
/ builtin arrays of FunkyItems that can be edited in Inspector / note: pressing play will cause the Start function to populate them */
var arrayTwo : FunkyItem []; var useMeInTheInspector : FunkyItem [];
/ example class /
class FunkyItem { var name : String; var descrip : String; var gameObj : GameObject;
/* default constructor - takes no arguments */
function FunkyItem ()
{
name = "Default Name";
descrip = "Default description";
}
/* a constructor that takes 3 arguments and assigns the GameObject */
function FunkyItem ( n : String, d : String, go : GameObject )
{
name = n;
descrip = d;
gameObj = go;
}
}
function Start () {
/* a dynamic 'javascript' array that could be used for any object */
var arrayOne = Array();
/* builtin arrays must be initialized */
/* they can be edited in the Inspector */
/* initialize the builtin array declared above */
arrayTwo = new FunkyItem [arraySize];
/* populate dynamic array using the default constructor */
for ( var i = 0; i < arraySize; i ++ ) {
var tempItem = new FunkyItem();
arrayOne.Push(tempItem);
}
/* populate builtin array using the constructor that takes 3 args */
for ( var j = 0; j < arraySize; j ++ ) {
var theName = "Item number " + j;
var theDesc = "They say " + theName + " is very funky";
/* create new FunkyItem using the object declared in Inspector */
var tempItem = new FunkyItem(theName, theDesc, testObject);
arrayTwo[j] = tempItem;
}
/* test it */
for ( var k = 0; k < arraySize; k ++ ) {
Debug.Log("Array one item " + k + " - " + arrayOne[k].name);
Debug.Log("Array two item " + k + " - " + arrayTwo[k].name);
}
/* how to turn ArrayOne into something you can use in the Inspector */
useMeInTheInspector = arrayOne.ToBuiltin(FunkyItem);
}
this helped a lot! it also showed some other stuff you can do with arrays. thanks.
You're very welcome. Glad I could help.
Using classes as much as possible is a great way to keep your code clean and easy to maintain.
Understanding the differences between dynamic arrays and builtin arrays is very important.
Great example, you just made my life a lot easier. Thnx
the line in 80-85 Debug.Log("Array one item " + k + " - " + arrayOne[k].name); gives an error 'name' is not a member of object. when i tried adding an function to access the 'name', i got the same error except this time it was for the function.
Help here..
Answer by SisterKy · Nov 21, 2011 at 02:48 AM
I'm not sure if I understand your problem correctly...
You want that what you posted there, just instead of 'var object: Transform' something like 'var object: MyThing'?
Just make a new .js file and name it 'MyThing' and for testing, add the line:
var stuff : 'string';
And then in your original script you should be able to do
var object1 : MyThing;
var object2 : MyThing;
object1.stuff = "hey!";
object2.stuff = "oi...";
var arr = new Array();
function Start () {
arr.Add(object1);
arr.Add(object2);
}
Bonus Info: If you want to be über, try wrapping
class MyThing extends ScriptableObject { }
around the whole code. [Note: I'm not 100% certain about the syntax/spelling...]
At this point, this doesn't influence how this class will work in your array.
But ScriptableObject is the intended root-class for all classes that just hold information and are not intended to be attached to a GameObject. [Beware! Trying to add these scripts as Components will cause problems!]
By contrast to the Class MonoBehaviour which is the baseclass for all self-made GameObject-Components.
(All .js files secretly add
class [the name of your .js file here] extends MonoBehaviour { }
and wrap your code if nothing else is specified)
Also, if you want to learn more about Arrays you might find this helpful: http://www.unifycommunity.com/wiki/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use%3F
Greetz, Ky.
this helped as well...i didn't know that you could just make a .js file and unity considered it a new class.