- Home /
Picking up an object
I am getting from what I can see a very bazar error telling me that AddToInventory has no receiver.
Sending the information script:
if (canLoot2 == true) {
if (GUI.Button( Rect( 280, 70, 50, 50), thing2Texture)) {
Instantiate(thing2, transform.position, Quaternion.identity);
thing2.SendMessage ("AddToInventory");
canLoot2 = false;
}
}
AddToInventory function (belongs to the item):
function AddToInventory () {
var inventory = player.GetComponent(Inventory);
if (inventory != null) {
inventory.AddItem(this);
isTrigger = true;
renderer.enabled = false;
transform.position = inventory.transform.position;
}
}
Answer by JinxM · Feb 21, 2012 at 01:16 AM
thing2 is the name of the prefab, not the name of the Instantiated instance of the prefab.
Change the 2 lines in the original code to:
var aThing = Instantiate(thing2, transform.position, Quaternion.identity); aThing.SendMessage ("AddToInventory");
Answer by jamiller · Feb 20, 2012 at 05:03 PM
I think you want to change your instantiate and sendMessage lines to look something like this:
var newThing = Instantiate(thing2, transform.position, Quaternion.identity);
newThing.SendMessage("AddToInventory");
thing2 is a reference to the object to be instantiated, not the newly instantiated object... you need to store the new object in a variable (newThing in my example)
Your answer
Follow this Question
Related Questions
Storing items in inventory 1 Answer
In what way can I make a GUI Inventory? 0 Answers
Increasing speed for a few seconds 2 Answers
moving a object from ground to inventory 1 Answer
Inventory System: "The given key was not present in the dictionary." 2 Answers