I have a normal game object that represents a lamp, and for it i have a script responsible for turning on/off, and then another script responsible for glowing or unglowing the lamp when it is on or off respectively.
The thing is, inside my turn on/off script I have a status variable (a boolean) that tells its current status (on/off), and I want the other script to have access to it. But how can I do such thing? Scripts can’t handle variables of other scripts, even if they’re attached to the same object so far i know.
Thanks!
//some script
public class Toggle : MonoBehaviour
{
...
public bool IsEnabled
{
return ...
}
}
//other script on same gameobject
public class Glower : MonoBehaviour
{
...
void GlowEffectApply()
{
bool lightEnabled = GetComponent().IsEnabled
}
}
Here’s a UnityScript example:
// Toggle script
// Toggle.js
var state = false;
// [...]
In your other script just do this if you want to read the state variable:
if (GetComponent(Toggle).state)
If you need to access the variable a lot, or you need other things from that component, save a reference to it. GetComponent have to search for the component on the gameobject eachtime you call it. It’s better to setup the references in Start and use the reference directly:
// another script on the same gameobject
private var myToggleScript : Toggle;
function Start()
{
myToggleScript = GetComponent(Toggle);
}
function Update()
{
if (myToggleScript.state)
{
// [...]
}
}
Note: I’m posting this as an answer because it is quite an answer to a question here and because I need the Code section or else the linebreaks will get messy.
I’ve get it, if I define a monobehaviour class inside a .js Unity will not make that conversion. Meaning that in file (1) I can put the OnOff class and then extend it in file (2) and it will behave correctly!
So in Unity having this in a “Example.js”:
------Code------
function start() {...}
function update() {...]
is EXACTLY the same as
having this in a “Example.js”:
------Code------
public class Example extends MonoBehaviour {
function start() {...}
function update() {...]
}
And the same goes for C#. This is awesome, now my code will get pretty! 
Thank you guys!