- Home /
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;
}
}
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]
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
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
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
Follow this Question
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