I am doing inventory system, and try to implement it in Classes way. But there are some problems.
class Inventory{
private var items:Item[]=new Item[GameData.player_max_slots];
function Inventory(){
print("1");
for(var i=0;i<GameData.player_max_slots;i++)
items*=null;*
*}*
*function Print_Item_Names(){*
*for(var i=0;i<GameData.player_max_slots;i++)*
_print(items*.GetName);*_
_*print("2");*_
_*}*_
<em>*function Add_Item(_item:Item)*</em>
_*{*_
<em>*for(var i=0;i<GameData.player_max_slots;i++)*</em>
<em><em>items*=_item;*</em></em>
<em>_*print("3");*_</em>
<em>_*}*_</em>
<em>_*```*_</em>
<em>_*<p>}</p>*_</em>
<em>_*```*_</em>
<em>_*class Item{*_</em>
<em>_*private var name:String;*_</em>
<em><em>*function Item(_name:String)*</em></em>
<em>_*{*_</em>
<em><em>*name=_name;*</em></em>
<em>_*}*_</em>
<em>_*function GetName():String{*_</em>
<em>_*return name;*_</em>
<em>_*}*_</em>
<em>_*```*_</em>
<em>_*<p>}</p>*_</em>
<em>_*<p>And I try to test it:</p>*_</em>
<em>_*```*_</em>
<em>_*if (Input.GetKeyDown ("q"))*_</em>
<em>_*{*_</em>
<em><em>*var player_inventory:Inventory=new Inventory();*</em></em>
<em><em>*player_inventory.Add_Item(new Item("ff"));*</em></em>
<em><em>*player_inventory.Print_Item_Names();*</em></em>
<em>_*}*_</em>
<em>_*```*_</em>
<em><em>*<p>Print_Item_Names() doesn't work.*</em></em>
<em>_*And even print("1"); print("2"); print("3"); in methods of the inventory Class are not called.</p>*_</em>
If you put a print("0"); in front of the first line, does IT print? No, it doesn't print. Is your script attached to an object that's enabled? 1. The 2 classes scripts doesn't attach to anything, do I need to? 2. The script of press "q" and print result is attached to an empty object, this function will execute. I have tested. Is it in an OnGUI or Update function (or something like it)? Yes, the press "q" function is in Update(); Thanks
– Dreamererr. why no line break...maybe need to press shift+enter? I try try 1 more time
– DreamerI typically attach scripts with OnGUI() or Update() functions to the GameObjects they interact with. In your case I would attach that script to an empty, enabled GameObject for testing purposes. Or to anything in your scene.
– jahroy