- Home /
How do I pass a script via inspector reference?
I have a function on a Character Controller that's called when it collides with any GameObject tagged ItemPickup, and calls the RequestPickUp method, passing the script, ItemPickup, from the colllided object.
function RequestPickUp(itemScript : ItemPickup)
{
if (canPickUp) {
itemScript.PickedUp();
inventoryObject.AddComponent(itemScript.item);
}
}
Now, what I want to do is add the class itemScript.item is referencing to a holder GameObject (inventoryObject). I'd also like to be able to attach extensions from InventoryItem as well.
ItemPickup.js:
var item : InventoryItem;
function PickedUp() {
Destroy(parent);
}
InventoryItem.js is so far an empty class extending from MonoBehaviour. It's attached to the same object as ItemPickup.js and is linked via inspector reference.
class InventoryItem extends MonoBehaviour
{
}
But when compile time rolls around... I get:
'No appropriate version of 'UnityEngine.GameObject.AddComponent' for the argument list '(InventoryItem)' was found.'
Answer by Montraydavis · Feb 25, 2013 at 07:25 PM
Well, I am not home to test out a C# script, but, here is the Javascript version of that code. Hopefully it won't be too hard to convert . . . .
//File name to access is OOP.js
var MyOOP : OOP ;
function Start ( )
{
MyOOP = GetComponent ( OOP ) ;
MyOOP.DoFunctionFromScript ( );
}
Pretty simple script, and I hope this solves your issue. Good luck, and let me know if this is not what you are looking for . . . . .
Edit/Update:
//pickup
var item : InventoryItem;
function PickedUp() {
Destroy(parent);
}
function Start ( )
{
item = GetComponent ( ItemInventory )
}
If this is not working, please post the exact output from the console, please.
Well, I'm writing it in Javascript, so no problems on that account :D.
The main problem I have is that I'm planning on destroying the GameObject ItemPickup.js is attached to, and ItemPickup.js has a reference to a (customized or extended from) InventoryItem.js script, also attached to the GameObject.
What I'm hoping to do is to AddComponent the InventoryItem (or something extended from InventoryItem) that I'm pointing at onto inventoryObject... whereupon it complains that AddComponent(itemScript.item) doesn't work.
Because you never accessed the component. Component for AddComponent needs to be referenced.
item = GetComponent ( InventoryItem ) ;
Add to Start ( ); ( Or anywhere before item is added as component )
Nope, still getting the same compile error:
BCE0023: No appropriate version of 'UnityEngine.GameObject.AddComponent' for the argument list '(InventoryItem)' was found.
Though I'm pretty sure it wouldn't matter whether that line's in Start(), since I assigned it via Inspector anyways, and the compile error shows it's definitely being passed on.
What REALLY annoys me is that changing the line to:
inventoryObject.AddComponent(InventoryItem);
works fine... but it's not what I want to do, since I'll need to add subclasses of InventoryItem, not just InventoryItem. Is there no clean way to clone components?
Your answer

Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Adding Prefab Components 1 Answer
Adding a component in the editor with a script 0 Answers
Destroying Object when Adding Component 2 Answers
Creating Hinge Joint at runtime? 1 Answer