x


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.

more ▼

asked Jul 05 '11 at 10:19 PM

Kith gravatar image

Kith
541 27 28 38

P.S. GodScript was written in Javascript. I don't know if that's relevant or not

Jul 05 '11 at 10:32 PM Kith

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.

Jul 05 '11 at 10:34 PM sharat

Meh...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.

Jul 05 '11 at 10:36 PM Kith

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.

Jul 05 '11 at 10:38 PM sharat

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

Jul 05 '11 at 10:38 PM Chris D
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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>();
more ▼

answered Jul 06 '11 at 08:17 PM

flaviusxvii gravatar image

flaviusxvii
3k 13 19 43

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4372
x41
x22
x14

asked: Jul 05 '11 at 10:19 PM

Seen: 2619 times

Last Updated: Jul 06 '11 at 08:17 PM