- Home /
Change a variable of an Instantiated object
Hi all,
When an enemy dies, he loots objects, in this case, a heart (being Instantiated) I would like to change the string "itemType" in the script "Item.cs" attached to this heart object
but I have no clue how to make this work
so an abreviation of the question is: how to change a variable of an Instantiated object (in C#)?
here is my actual instantiation code:
Rigidbody InstantiatedHeart = Instantiate(heart, gameObject.transform.position, gameObject.transform.rotation) as Rigidbody;
Item item = gameObject.GetComponent<Item>;
item.itemType = "Heart";
But I get an error on the second line: " error CS0428: Cannot convert method group GetComponent' to non-delegate type
Item'. Consider using parentheses to invoke the method "
but I dont understant what I must modify... Thank you all for any help possible
Answer by GrKl · Aug 26, 2013 at 09:00 AM
Ok, I managed to solve my problem like this:
Rigidbody InstantiatedHeart = Instantiate(heart, gameObject.transform.position, gameObject.transform.rotation) as Rigidbody;
InstantiatedHeart.gameObject.GetComponent<Item>().itemType = "Heart";
thanks for help
Answer by DaveA · Aug 25, 2013 at 10:18 PM
Item item = InstantiatedHeart.gameObject.GetComponent;
also you may be getting conflicts with a system object called Item? I would rename that script to something more specific like InventoryItem to avoid name clashes.
Thanks, but actually, ive tried this too but then get error:
" error CS0428: Cannot convert method group GetComponent' to non-delegate type
Item'. Consider using parentheses to invoke the method "