- Home /
How to access a function from a script on another object
I'm trying to access a function that is in a js script on another object. I know there are lots of exaples and questions like this but none of them solve my problem and I have no idea why.
ex. http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
I have an object that is a "slot" and another object that is an "item". I have a function on my "item" script that will drop it into the slot. All of this works fine but I want to execute a function on the slot after dropping it (Since all slots will have a different function). Here is my code:
//slot is a GameObject already stored in slot
slot.GetComponent('SlotControler').DropItem();
//SlotControler is the name of the script on the slot object
//DropItem() is the name of the function I want to execute
There error I recieve is: BCE0019: 'DropItem' is not a member of 'UnityEngine.Component'.
Any help as to how I could fix this is greatly apreciated. Thanks in advance!
Answer by whydoidoit · May 02, 2013 at 06:57 PM
Don't know where you've been looking ;) Don't use the version of GetComponent which takes a string, it just returns a Component
slot.GetComponent(SlotControler).DropItem();
Wow thanks for super fast reply! Now I feel like an idiot so much time lost with this :D Well at least I will learn from my mistakes, works great now!
It does get everyone that :D It's probably "cant see the wood for the trees" problem when searching I bet.
Your answer
Follow this Question
Related Questions
How do I make a sword swing? 2 Answers
How to display GUI in sequence in trigger 3 Answers
script linking 1 Answer
problem at adding script 1 Answer