Class Programming to build an inventory system

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>

Sounds like the constructor is not getting called.

If you put a print("0"); in front of the first line, does IT print?

Is your script attached to an object that's enabled?

Is it in an OnGUI or Update function (or something like it)?

First of all, you don't need to initialize reference type variables to null. I.e., in your Inventory constructor, don't loop through the array, setting items*=null;. When you create an empty static array of objects, the elements are automatically null.

* *

That said, I'm not sure why at least print("1") wouldn't get called. All other errors aside, when you hit q, the first thing that happens is that the Inventory constructor is invoked, which calls print("1") as its very first action. Therefore, I suspect there's a problem with this code's location in your program. Where have you put the if-statement testing for the q-key? Is it in some script's Update? Is that script enabled?

*

Classes that don't derive from MonoBehaviour don't properly recognize the `print` function, though I don't remember why anymore. For whatever reason they don't throw an error on it, but it just doesn't do anything. You can use `Debug.Log` and it will work, or you can make your class derive from MonoBehaviour - depending on your needs.