• 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
1
Question by frost914 · Feb 16, 2013 at 12:22 PM · arrays

Trying to create a custom class array

I'm kinda new to Unity and Javascript and working on a simple roguelike game. I'm trying to create an array of a custom "Weapon" class but getting error messages. The code goes like this:

 class Weapon {
     var name : String;
     var minDamage : int;
     var maxDamage : int;
     var textureOffset : Vector2;
 };
 
 var weaponList : Weapon[];
 
 function Start () {
     initWeaponList();
 }
 
 function initWeaponList () {
 
     weaponList[0].name = "Branch";
     weaponList[0].minDamage = 1;
     weaponList[0].maxDamage = 1;
     weaponList[0].textureOffset = Vector2(0,0);
     
     weaponList[1].name = "Short Sword";
     weaponList[1].minDamage = 1;
     weaponList[1].maxDamage = 2;
     weaponList[1].textureOffset = Vector2(0.125,0);
 }

What's wrong with this code? I've checked around on this board and in the Unity script references, and tried a couple other ways to do this, but nothing seems to work. Help would be appreciated!

Comment
Add comment · Show 3
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 Chronos-L · Feb 16, 2013 at 01:16 PM 0
Share

What's the error message? Always post your error message when you need help to figure out what's wrong with the code. $$anonymous$$ost of us will not go as far as re-creating your script in our project just to help you fix it.

avatar image sacredgeometry · Feb 16, 2013 at 06:46 PM 0
Share

On a side note.

I would build a constructor/initialiser for your weapon class. It will really speed up populating the weapons list and make your code smaller/more readable.

avatar image sacredgeometry · Feb 16, 2013 at 06:57 PM 0
Share

http://pastebin.com/fvauA0TR

here is some code explaining what I mean, my js is a little shoddy as I use c# but I hope it makes sense.

the two other functions are a default constructor and a overridden one...it basically works on what parameters the function receives.

When you instantiate a class without parameters it calls the first weapon class .. which in turn calls the other constructor and sets all the class variables to null.

when you add the properties, as I have in initWeaponList, when instantiating the weapon objects for the weapon list. It allows you to create instances already initialised.

I hope that helps.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by MarkFinn · Feb 16, 2013 at 02:04 PM

You need to initialise the array. As it stands, you have the name of an array, with no array attached.

 function initWeaponList () {
     weaponList=new Weapon[2];
     weaponList[0]=new Weapon();
     weaponList[0].name = "Branch";
     weaponList[0].minDamage = 1;
     weaponList[0].maxDamage = 1;
     weaponList[0].textureOffset = Vector2(0,0);
      
     weaponList[1]=new Weapon();
     weaponList[1].name = "Short Sword";
     weaponList[1].minDamage = 1;
     weaponList[1].maxDamage = 2;
     weaponList[1].textureOffset = Vector2(0.125,0);</PRE>
 }
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 frost914 · Feb 16, 2013 at 04:21 PM 0
Share

Thank you very much! This explains a lot since I've always omitted this vital step.

avatar image sacredgeometry · Feb 16, 2013 at 06:39 PM 0
Share

wouldn't the scope of that array be limited to the function this way? I assume he/she wants access to the array outside of the function as its a list of the weapons i am guessing the protagonist or npcs is/are carrying.

avatar image Eric5h5 · Feb 16, 2013 at 06:50 PM 1
Share

@sacredgeometry: no, the variable is declared outside the function so it's global.

avatar image sacredgeometry · Feb 16, 2013 at 07:21 PM 0
Share

Are there mutable lists in js? if so why cant you initialise non specific sized list in the declaration?

avatar image Eric5h5 · Feb 16, 2013 at 08:50 PM 1
Share

Everything in Unity is built on .NET/$$anonymous$$ono, so the language isn't relevant as far as what methods/collections are supported. If you don't need the size of the array to be dynamic then it's better to use standard arrays since they are faster than List.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

hi!does any know how to make an endless track 1 Answer

Get ParticleSystem (Shuriken) to play from array of game objects C# 2 Answers

How to find the index of an Object in an Array 2 Answers

Advance through array elements with math (++)? 1 Answer

Attempt at creating an array of text files results in different errors 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