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
2
Question by Ruben 2 · Nov 21, 2011 at 01:49 AM · arrayobjectarraysclassclasses

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?

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

2 Replies

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

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);

}

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 Ruben 2 · Nov 22, 2011 at 10:17 PM 0
Share

this helped a lot! it also showed some other stuff you can do with arrays. thanks.

avatar image jahroy · Nov 22, 2011 at 11:25 PM 0
Share

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.

avatar image Hardcore Games · Apr 17, 2013 at 11:40 PM 0
Share

Great example, you just made my life a lot easier. Thnx

avatar image Krish-Vikram · Jan 21, 2014 at 01:18 PM 0
Share

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..

avatar image
1

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.

Comment
Add comment · Show 1 · 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 Ruben 2 · Nov 22, 2011 at 10:19 PM 0
Share

this helped as well...i didn't know that you could just make a .js file and unity considered it a new class.

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

6 People are following this question.

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

Related Questions

Instantiating objects from a class? (C#) 1 Answer

Getting an error trying to assign a value to an array of classes element 0 Answers

Array empties values on RunTime? 1 Answer

array of objects 1 Answer

Instantiating gameObject with custom Class properties 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