x


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;

}

}
more ▼

asked Feb 20 '12 at 02:36 PM

MithosAnnar gravatar image

MithosAnnar
299 22 36 40

bizarre*

Feb 21 '12 at 02:07 PM MithosAnnar
(comments are locked)
10|3000 characters needed characters left

2 answers: sort oldest

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");

more ▼

answered Feb 21 '12 at 01:16 AM

JinxM gravatar image

JinxM
226 1 3

Thanks a lot!

Feb 21 '12 at 10:09 AM MithosAnnar
(comments are locked)
10|3000 characters needed characters left

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)

more ▼

answered Feb 20 '12 at 05:03 PM

jamiller gravatar image

jamiller
53 2 3 8

This is correct to +1!

Feb 21 '12 at 10:10 AM MithosAnnar
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1095
x255
x224
x68
x23

asked: Feb 20 '12 at 02:36 PM

Seen: 748 times

Last Updated: Feb 21 '12 at 02:07 PM