Quantcast
Channel: Latest Questions by Bob423
Viewing all articles
Browse latest Browse all 12

Static functions and lists not co-operating.

$
0
0
I've managed a simple inventory system using lists and it works pretty well. The problem is that static variables and functions don't seem to work with it. It works perfectly if there are no static variables, but that also means I can't add an item to the list from a different script. import System.Collections.Generic; static var ItemName : List.; static var ItemAmount : List.; static var Index : int; // Index of item. i.e: Where it is in the list. static var Items : int; // Number of item types in inventory static function AddItem(item, amount){ if(ItemName.Contains(item)){ // Does the player have the item? Index = ItemName.IndexOf(item); ItemAmount[Index] += amount; // Increase number of items by number }else{ // If the player does not have the item... ItemName.Add(item); // Add Item to the list ItemAmount.Add(amount); } } static function RemoveItem(item, amount){ if(ItemName.Contains(item)){ Index = ItemName.IndexOf(item); ItemAmount[Index] -= amount; if(ItemAmount[Index] <=0){ // If player has 0 or less of the item ItemName.RemoveAt(Index); ItemAmount.RemoveAt(Index); } } } function ShowItem(amount){ if(Items >= amount){ GUI.Label(Rect(150,20 * amount,200,21), ItemName[amount - 1] + " x" + ItemAmount[amount - 1]); } } function OnGUI(){ var ItemTypes : int; for(ItemTypes = 1; ItemTypes < 124; ItemTypes++){ ShowItem(ItemTypes); } } function Update(){ Inventory.Items = Inventory.ItemAmount.Count; } I keep getting the always annoying "NullReferenceException: Object reference not set to an instance of an object" error when the script tries to update. Like I said, making the variables private fixes it, but I need the variables to not be private. Is there another way of doing this, or something I'm missing?

Viewing all articles
Browse latest Browse all 12

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>