- Home /
C# Referencing other Script
Hello again, I'm very new to C#, but desperately want to start learning. However, I'm having some trouble with something that I'm pretty sure is very simple.
In Javascript, I could easily do this...
private var GameGod : GameObject;
private var GameScript : GodScript;
function Start() {
GameGod = GameObject.Find("Game God");
GameScript = GameGod.GetComponent("GodScript");
}
However, when I try to do something similar in C#
private GameObject GameGod;
private GodScript GameScript;
void Start () {
GameGod = GameObject.Find("Game God");
GameScript = GameGod.GetComponent("GodScript");
}
I get an error saying "The type or namespace name "GodScript" could not be found. Are you missing a using directive or an assembly reference?"
I'm not sure what's wrong, but I have a feeling I'll feel stupid when I find out lol.
P.S. GodScript was written in Javascript. I don't know if that's relevant or not
That definitely has something to do with it. I remember reading something about the compile order of Javascript and C# on the docs somewhere, but there can be problems when you mix the two.
$$anonymous$$eh...I'll just rewrite this script in Javascript I guess. That's what I get for wanting to learn C# halfway through a game project lol.
Here it is.
http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html
I would say it probably is easier to start with independent scripts, but you can affect the order things are compiled by changing the location. Otherwise you can't have C# dependent on JavaScript or vice-versa.
Is your GodScript type being defined anywhere the script can find it? If you're mixing and matching languages, take a look here to see what you have to worry about in terms of compile order.
edit: ya, what @sharat said
Answer by flaviusxvii · Jul 06, 2011 at 08:17 PM
First and foremost, try not to start your variable names with upper case letters. It makes it hard to distinguish them from types.
You probably need to do this:
gameScript = gameGod.GetComponent<GodScript>();
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
is it possible to give only part of array through [] 1 Answer