I am doing an inventory table. My player is able to pickup item, I want the item picked up to be stored into the inventory, how do I pass the item.
I have tried the following:
//the item generated randomly
var obj2 = Instantiate (theObjects[Random.Range(0,14)], holefront, Quaternion.identity);
//pass the item to my LootTable script
var myPickUpItem : LootTable = GetComponent (LootTable);
// store into the array of lootContent
myPickUpItem.NewLootContent(obj2);
It doesn’t work I am not sure how to pass it. Please correct me…
Thank you.
I don’t think there’s enough info there to say what the problem is. If you need help with this, you’ll probably need to describe in detail what ‘doesn’t work’ means, and also post all of the relevant code (e.g. the code for the LootTable script).
What i meant doesn’t work is, the item that is picked is not passed to the LootTable script. How do I perform this?
The following is the LootTable script:
function NewLootContent (thisLootableItem : LootableObject)
{
var contentArray = new InventoryItem ();
for (var i : int = 0; i < contentArray.length; i ++) {
var newLootableItem = new InventoryItem();
newLootableItem = lootArray[Random.Range(0,lootArray.length)];
contentArray[i] = newLootableItem;
}
return contentArray;
}
You pass the item as the parameter thisLootableItem, but it is never used in the function. Could that be the reason?
I did a different approach this time, when the object collides I pass it to the LootTable,
function OnControllerColliderHit(hit:ControllerColliderHit)
{
if(hit.gameObject.tag == "inventory")
{
Debug.Log("You picked up the item!");
var myItem : LootTable = GetComponent(LootTable);
myItem.GetItem(hit.gameObject);
Destroy(hit.gameObject);
}
}
In my LootTable, i just did a function to test if it is passed:
function GetItem(obj)
{
Debug.Log("ITEM: " + obj);
}
But then I get the following in the console:
NullReferenceException: Object reference not set to an instance of an object
PlayerRelativeControl.OnControllerColliderHit (UnityEngine.ControllerColliderHit hit) (at Assets/Standard Assets (Mobile)/Scripts/PlayerRelativeControl.js:282)
UnityEngine.CharacterController:Move(Vector3)
where line 282 is this → Destroy(hit.gameObject);
Is it so difficult to pass an object from a script to another? If this is not right is there another way for me to do this?
I did a different approach this time, when the object collides I pass it to the LootTable,
function OnControllerColliderHit(hit:ControllerColliderHit)
{
if(hit.gameObject.tag == "inventory")
{
Debug.Log("You picked up the item!");
var myItem : LootTable = GetComponent(LootTable);
myItem.GetItem(hit.gameObject);
Destroy(hit.gameObject);
}
}
In my LootTable, i just did a function to test if it is passed:
function GetItem(obj)
{
Debug.Log("ITEM: " + obj);
}
But then I get the following in the console:
NullReferenceException: Object reference not set to an instance of an object
PlayerRelativeControl.OnControllerColliderHit (UnityEngine.ControllerColliderHit hit) (at Assets/Standard Assets (Mobile)/Scripts/PlayerRelativeControl.js:282)
UnityEngine.CharacterController:Move(Vector3)
where line 282 is this → Destroy(hit.gameObject);
Is it so difficult to pass an object from a script to another? If this is not right is there another way for me to do this?
Your line:
var myItem : LootTable = GetComponent(LootTable);
Is checking for the LootTable attached to “this”. That is, not the “hit.gameObject”, but the object to which this script is attached to. If this is intended (it might be since you then destroy your inventory object?), then double-check that this current script actually has a LootTable script attached to it.
Thanks for your response.
I have question, I have so many different object on the ground my player can pick. When it collides which I will recognize the object through hit.gameObject. If I dont do that how can i identify my object.
I have removed the destroy code, but I still get the same error.
What you mean by this “then double-check that this current script actually has a LootTable script attached to it.”?
var myItem : LootTable = GetComponent(LootTable);
Debug.Log("is LootTable assigned? " + (myItem != null));
If that comes up “false”, it means you have not properly attached the LootTable script to your character.
I attached the script to each of my item, I have change it to my character.
I get true as the result. Thanks for that.
Now How do I pass the item to the LootTable in order to store it in the inventory?
I manage to pass the name of the object pickedup to the LootTable through the Debug.Log. How do I store the object texture into the inventory table.
Please guide me on doing this.
Well you seem to already have that knowledge essentially, just alter the properties or call functions on a target script. There are some more examples here: Unity - Scripting API: GameObject.GetComponent
I am passing the object to the LootTable using the following script:
function Update () {
var item = new InventoryItem1();
item.worldObject = gameObject;
item.itemname = itemname;
item.itemtype = itemtype;
item.equipmenttype = equipmenttype;
item.usable = usable;
item.itemweight = itemweight;
item.droppable = droppable;
item.itemmodel = itemmodel;
item.itemstacksize = itemstacksize;
item.itemstacklimit = itemstacklimit;
item.showStack = showStack;
item.bagsize = bagsize;
item.showBag = showBag;
item.BagItem = BagItem;
Debug.Log("Item pick: " + item.itemname);
LootTable.statLoot.AddItem(item);
}
Then in my LootTable script
function AddItem( item : InventoryItem1 )
{
Debug.Log("ColideItem " + item);
if (item.itemtype == "Money"){
if(item.itemname == "Copper"){
if (inventorymoney[0] == null){
inventorymoney[0] = item;
Debug.Log("MONEY " + item.itemname);
Destroy (item.worldObject);
return;
} else {
inventorymoney[0].itemstacksize = inventorymoney[0].itemstacksize + item.itemstacksize;
Destroy (item.worldObject);
return;
}
}
}
}
The first time I did i got on the console the name of the item but i also get the following error:
NullReferenceException: Object reference not set to an instance of an object
ItemScript.Update () (at Assets/MyScript/ItemScript.js:54)
the line is → LootTable.statLoot.AddItem(item);
What is this error means?