Javascript - Generic List question
Hello, first time writer, long time reader.
Javascript code.
I am trying to construct a inventory system. I was following along some guides and came to a point where I was stuck. The video was using C#, so I was attempting to convert from C# to JS as I went along which worked until I came to the below issue.
I have a ItemDatabase that uses a generic List "Items" that I can edit in the inspector.
I'm trying to pull the variables from that list to another script. I can't seem to figure this out and searching generic lists in google gives a ton of answer that are not helpful here.
this is my list in script Database, it's pulling the variables from script "Items".
var items : List. = new List.();
This is a sample of the variables in the script "Items";
public var itemID = 0; public var itemAttack = 0;
I then go and set the values for each item in the inspector. This allows me to easily grow the list as I go along.
Now say i want to grab the itemAttack value for calculating attack, how could I grab that?
If I use items[1].itemAttack (the item I'm trying to use being the second item in the list) in the script Database it pulls just fine, I've tested with Debug.Log. However if I try to utilize that in another script it does not. I usually get Object does not support slicing.
I was thinking that in the script Database it knows it's a list and therefore can read the [] and know what I am asking. However if I try to pull it in another script I figured the script didn't know there were variables stored in the list so it wasn't pulling them.
Do I need to declare something in every script I intend to have the values set in?
I tried to set a var ItemList to GameObject.Find("ItemDatabase").GetComponent(Database).items;
but that seems to set that var to a Object rather then the list.
Any help would be appreciated. I've spent most of my weekend reading and I'm not finding a solution.
Thank you,
You could turn your list into an array and access it that way
items.ToArray()[1]
Your answer
Follow this Question
Related Questions
What part of my camera script is increasing my cameras X rotation? 1 Answer
how do I rezolve "look rotation viewing vector is zero"? 1 Answer
Moving Platform 3D 1 Answer
Raycasting Animations 0 Answers
Button problem (noob) 1 Answer