- Home /
Is not a member of object
I seem to know the reason why i am getting this error but i dont know how another way to solve this, the problem is giving error saying "name" and "quantity" is not a member of this object.
internal var myDrugs = new Array();
function buyDrug(drug : Drug) {
myDrugs.push(drug);
}
function getDrugs(index : int) {
return myDrugs[index];
}
function getDrugsLength() {
return myDrugs.length;
}
function sellDrug(name : String, amount : int) {
for (var i = 0; i < myDrugs.length; i++) {
if (myDrugs[i].name == name) {
if (myDrugs[i].quantity <= amount) {
myDrugs[i].quantity -= amount;
}
if (myDrugs[i].quantity == 0) {
myDrugs.RemoveAt(i);
}
}
}
}
Answer by robertbu · Jan 17, 2014 at 05:37 PM
Don't use the 'Array' class. For what you are doing here, the .NET List class is a good choice. More information on the various collection types:
http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?
To use the List class you will need this at the top of the file:
import System.Collections.Generic;
and you will declare it:
var myDrugs : List.<Drug> = new List.<Drug>();
And it will be 'Add' not 'push' to put them in the list.
Your answer
![](https://koobas.hobune.stream/wayback/20220613130713im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Creating materials that look like real objects 2 Answers
Grouping Enemies/Targets 1 Answer
edit values for multiple objects concurrently 1 Answer
Collectable Hints 2 Answers
Accessing other objects 2 Answers