- Home /
GetComponent from JS to c# returns null
I'm very new at this so try and forgive me.
I'm trying to get a variable from a javascript file over to a c# file. I try calling gameObject.GetComponent< javascriptfile >() from the c# file, but it always returns null. Since I've heard that c# compiles before js, I put the js file in Standard Assets. Is that why it's returning null? Or is it because it's a different script types?
//StoryController.cs GameLobby lobbyScript; void Start () { lobbyScript = gameObject.GetComponent< GameLobby>(); // returns null }
//GameLobby.js
Does your gameobject definitely have the GameLobby script attached to it?
So you need to make sure both StoryController.cs and GameLobby.js are attached to the same object.
Also, if you post the code for GameLobby.js i can convert it to C# for you if you like? I think it's always a good idea to keep all files the same type.
Answer by Ben-Stoneman · Jun 11, 2013 at 04:16 PM
Add the object with the lobbyscript to the StoryController in the inspector (Make sure the lobbyScript is public so that it displayes in the inspector).
Also see here for referencing correctly: http://answers.unity3d.com/questions/184738/javascript-call-a-method-from-c-sharp-and-vise-ver.html
Your answer
Follow this Question
Related Questions
How could i convert this from java to #c 1 Answer
What am I doing wrong? (converting a javascript code to c#) 1 Answer
Copy collider Component type and parameter to a new Object at runtime 0 Answers
Loop through Children Unable to Access Component Script 1 Answer
public variable vs GetComponent() 2 Answers