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 BlackHoleStorm · Aug 26, 2014 at 10:09 AM · c#arrayclass

Array of a custom class giving an error

Hi

I've created a class called IWeapon and when I try to make an array of it, I get the following error "Cannot apply indexing with [] to an expression of type 'IWeapon'" can anyone tell me what is wrong please? Here are the scripts for IWeapon and IItem, which it inherits from

 //IWeapon.cs
 public class IWeapon : IItem
 {
     public enum Type
     {
         Sword,            //0
         Lance,            //1
         Staff,            //2
         Bow,            //3
         Axe,            //4
         Hammer,            //5
     };
     public Type type;
     public int atk;
 
     public IWeapon()
     {
     }
 
     public IWeapon(string wname, string wdesc, IWeapon.Type wtype, int watk)
     {
         itemName = wname;
         description = wdesc;
         type = wtype;
         atk = watk;
     }
 }
 
 //IItem.cs
 public class IItem
 {
     public string itemName;
     public string description;
 
     public IItem()
     {
     }
 
     public IItem(string iname, string idesc)
     {
         itemName = iname;
         description = idesc;
     }
 }
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
1
Best Answer

Answer by NeverEndingPrjct · Aug 26, 2014 at 10:20 AM

u use only I for Interfaces ... if u writing an class remove the I ... Interfaces arent classes but they implement an standart behavior for ther classes

Type is alredy an Class ... i think there are Problems with the namespaces,because u have now 2 options for Type (ure enum and the .getTpy()// typof())...fix this...then u can call the base constructor...so u havnt to implement all the stuff ureself

It looks like

 public Weapon(string wname, string wdesc, IWeapon.Type wtype, int watk):base(wname,wdesc)
 {
 //type = wtype; //
 atk = watk;
 }

FINALY something is with ure syntax wrong. Indexing is that u can index an object like an array like Instance[]. For that behavior u have to iplement a this method...google it ore use the msdn...But u dont want to index u want an array so Weapon[] wpn = Weapon[Xxx]

Comment
Add comment · Show 3 · 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 BlackHoleStorm · Aug 26, 2014 at 10:40 AM 0
Share

Over here it says its good practice to use them for dependency injectables, I've been trying to follow these to make everything a little bit neater and the scripts a little less clunked up.

I've changed Type to WeaponType so now I have that and type which should fix having both problems, also which method should I Google?

Thanks for the help btw

EDIT: I just reread it properly and saw the single responsibility bit. I'll change them over now

avatar image BlackHoleStorm · Aug 26, 2014 at 11:01 AM 0
Share

I found the culprit, I skim read the debugger, it was later on in my script (I had weapon[i] = null when it should have been weapons[i] = null, I missed the s)

Thanks for the help again man

avatar image NeverEndingPrjct · Aug 26, 2014 at 11:37 AM 0
Share

i only know c# ,not so much about the engine but i is only used for interfaces everywhere...the whole ms api use i for interfaces fe icloneable icompareable ienumertor etc

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Set default length for an array of elements of a custom class in inspector 0 Answers

Script Wrapper Class 1 Answer

Why is my C# array of objects fully populated when some of them should be null? 3 Answers

How do i access the variables of a class that is in an array? 1 Answer

Need my function to work with different lists of different values (classes) 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