Getting public vars from other GO scripts

Im sure this has been asked before, but ive trawled the forum and just become totally confused... :roll:

I have a gameobject called mnu_Login which has a script called GenericGUIMethods attached. That script has a public int called TextFieldTabCurrent which i need to read from another GO's script.

for the life of me i cant figure out how to do it...

i thought it would be something like:

GenericGUIMethods TextFieldTabCurrent = TopLevelParent.GetComponent(typeof(GenericGUIMethods)) as GenericGUIMethods;

but im obviously way off track, because TextFieldTabCurrent just returns "GenericGUIMethods" - Im not a c# expert so a descriptive answer would be really good :)

thanks a bunch
Shaun

I can help via .js and you can translate

var varGameObject : GameObject;
varGameObject = gameObject.Find("TheGameObject");

var varScript : TheScriptName;
varScript = varGameObject.GetComponent(TheScript);

//then you can

varScript.parameter = yourValue;

Thanks for that - funny thing is I have it working perfectly in JS, i hit this problem because im converting all my scripts to C#

Its so simple to grab other scripts variables in JS, but seems to be a convoluted process in C# :?

FYI - problem solved - thanks to the Unity IRC channel (its fun, try it!)

If anyone has problems with this - To get any variables from C# script attached to any GO, use the following:

To create a reference to the script:

OtherScriptName scriptReference = (OtherScriptName)OtherGOName.GetComponent(typeof(OtherScriptName));

To access a public member variable of the script:

Debug.Log(scriptReference.TheVariableName);

OtherScriptName = the script that is attached to your other Game Object that contains the public memeber you want to access (it MUST be public)

scriptReference = the 'internal' reference to the other Game Objects script that you use to access its member variables - this can be anything you like, but in keeping with Unity standards, should be the same name as "OtherScriptName" with just the first letter in lower case.

OtherGOName = The name of the other game object that contains the script you want to access (this has to be of the type GameObject) - i used the inspector to manually define the object. Use the code "public GameObject OtherGOName = null;"

I hope this can save someone else some frustration - I wish the Unity script reference had C# examples too - oh well.

Cheers
Shaun

You can also simplify it to one call in C# like this:

//using find
MyScript script = GameObject.Find("whatever").GetComponent(typeof(MyScript)) as MyScript;

And so on. Sometimes easier when you don't really need the reference of the object the script is on.

-Jeremy

I knew you would have a better answer Jeremy ;)
damn your coding knowledge! 8)

Why have you decided to switch to C#? Does C# run significantly faster?

Or is it a networking thingy?

C# IMHO is a better language - and I'm liking it more everyday. To be sure there are some strange quirks and some syntax thats confusing, but C# grows on you ;) Also, my backend server code is .NET, so it makes sense to keep the same language.

Not sure about the speed, but certainly accessing .NET library from C# is much more intuitive than from JS - you can literally copy and paste examples from C# references and they just work (mostly).

Cheers
Shaun


What about C# makes it a better language in your opinion?
[quote]
but C# grows on you ;)
[/quote]
Perhaps some. Maybe I am just crochety ;) It isn't a bad language though.
[quote]
Also, my backend server code is .NET, so it makes sense to keep the same language.
[/quote]
That does make sense.
[quote]
Not sure about the speed,
[/quote]
Depends. If you are using dynamic typing in JS then it will be slower. If you static type then the speed should be about the same.
[quote]
you can literally copy and paste examples from C# references and they just work (mostly).
[/quote]
That is a definite advantage. Don't know if that really helps much on the learning part though ;)


Depends. If you are using dynamic typing in JS then it will be slower. If you static type then the speed should be about the same.
[/quote]
With Mono, all script code is compiled to the same bytecode anyway, so static typed, dynamic typed, it should only make a difference in compile speed, not runtime speed. In theory anyway.

The thing with C# is that it just has certain OOP features that are more robust, and a lot of people (including me) like the syntax better than JS.

I would break down and cry if I had to do my stuff in JS, simply because I like to code black-box classes and I personally find C# more suitable for that.

-Jeremy


Depends. If you are using dynamic typing in JS then it will be slower. If you static type then the speed should be about the same.
[/quote]
With Mono, all script code is compiled to the same bytecode anyway, so static typed, dynamic typed, it should only make a difference in compile speed, not runtime speed. In theory anyway.
[/quote]

Quite a few users confuse dynamic typing with the inferred typing used in Javascript. The inferred typing is when the Javascript compiler detects what type a function returns and automatically assigns that type to a variable. This is static typing even though the type is not mentioned explicitly in the source file and just as fast. Dynamic typing on the other hand is what happens if you do not specify an explicit type and the compiler can not detect the type during compile time. (This happens for instance when you call GetComponent or any other function that returns Component, UnityEngine.Object or System.Object.) Dynamic typing does incur a runtime overhead, as the code will have to look up any object members at runtime.

To disable dynamic typing (but still keep type inference.) You can add #pragma strict to the start of the JavaScript file. This will cause the compilation to fail in places where the compiler can't detect the variable type automatically instead of generating dynamicly typed code.