- Home /
Reference a function from another object
I know this has been asked a lot, but none of the other questions have worked. I have a function called getPlayer() in a spawn script which returns the player. In the first line, I'm trying to get the object that has the player script, but it doesn't let me drag a GameObject from in the scene into it. var SpawnLoc : GameObject; private var spawn : MonoBehaviour; spawn = SpawnLoc.GetComponent(Spawn);
I don't see anything wrong with your code. I ran a quick test, and I was a able to drag a game object onto SpawnLoc in the inspector. If you are having trouble, post more of your script. Please format the code using the button labeled 101/010.
Generally speaking it is not a good idea to call things like GetComponent to initialize a script variable - try moving the code to Start or Awake
Answer by Pysassin · Feb 05, 2013 at 10:48 AM
Because you cannot convert a monobehavior to a script. If you just need the script just use...
var variableName : Scriptname;
Then drag the object wih that script to it in the inspector and unity will handle it from there