- Home /
Share variables among scripts of the same object
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!
Why can't I up-vote answers? It gives me this stupid message: http://i45.tinypic.com/302uh51.png
You can't upvote because you don't have enough karma yet. If everyone can vote up, that would enable spammers to upvote their own spam posts from another account. That's why there's a 15 karma threshold.
Furthermore as long as you have less than 15 karma all your posts will be placed on the moderation queue for verification. This has been implemented to stop the spam on this site.
Next thing is, don't post such annotations as answer. Answers should answer the question. This is a Q&A site, not a forum ;)
I've converted your answer into a comment.
$$anonymous$$ost of these things are explained in the FAQs
> Scripts can't handle variables of other scripts, even if they're attached to the same object so far i know
http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
Answer by Bunny83 · Aug 17, 2012 at 07:45 PM
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)
{
// [...]
}
}
By the way, what if I don't know what's the name of the class I'm looking for, what would you put ins$$anonymous$$d of "Toggle" if you didn't know the name of the class?
Because I have a bunch of different scripts (classes) that have that variable, but the obvious way to do it is to make GetComponent() looking for each class to look the one used by the current object.
By other hand, how can I in this conditions in Javascript create inheritance between scripts, for example I have 2 files, "OnOff.js" (1) and "OnOffLamp.js" (2). File (1) defines that variable, and file (2) inherits from file (1). In C# it would be public class OnOffLamp : OnOff { ... } So that when I wanted to get the shared variable I would just need to do GetComponent(OnOff) ins$$anonymous$$d of GetComponent(OnOffLamp) that is too specific.
How can I achieve this? If you say that in background Unity transforms our JS into a class? $$anonymous$$eaning that probably putting something like this in a .js file won't make sense:
class $$anonymous$$yCoolObject extends $$anonymous$$onoBehaviour { var myCoolInt : int; var myCoolFloat : float }
Thanks!
Answer by ScroodgeM · Aug 17, 2012 at 03:30 PM
//some script
public class Toggle : MonoBehaviour { ... public bool IsEnabled { return ... } }
//other script on same gameobject
public class Glower : MonoBehaviour { ... void GlowEffectApply() { bool lightEnabled = GetComponent<Toggle>().IsEnabled } }
That's for C#, I'm using JS. If I use a class in a "script.js" attached to an object, where do I instatiate that class then, and where do I put special methods like start() and update()?
Can you do an example like that for JS? And please place start() and update() somewhere so that I can understand where they need to be placed. I know both languages but I'm quite confused about Unity's script attachment logic.
Thanks!
You also use classes in UnityScript. You just don't see the class construct because they are automatically generated behind the scenes. Start and Update are class methods
@Ne$$anonymous$$ewSys, if you know both languages, you should know that there's no difference in OOP (like C# is) where do you place methods in code.
if you use js (i don't recommend it if you know C#), the class name is your file name (without '.js')
I was confused about the way Unity forces JS plain files to be classes, and how that affected both C# and JS classes. Now in JS I'm declaring the class manually (with public class NameOfTheFile {) so that I can have a better feeling around this and make the code cleaner. I also preffer C# by far but unfortunatly I have to use JS in this project... :(
Answer by NeMewSys · Aug 18, 2012 at 12:00 PM
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! :D
Thank you guys!
You can post formatted code in comments, the same way it's done with answers (4 spaces in front of each line).
ah nice! Thanks!
function test() {
return null;
}
Gotta love this community :D