|
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!
(comments are locked)
|
|
Here's a UnityScript example: In your other script just do this if you want to read the state variable: 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: Thank you so much! :)
Aug 17 '12 at 10:23 PM
NeMewSys
By the way, what if I don't know what's the name of the class I'm looking for, what would you put instead 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) instead 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? Meaning that probably putting something like this in a .js file won't make sense: class MyCoolObject extends MonoBehaviour { var myCoolInt : int; var myCoolFloat : float } Thanks!
Aug 18 '12 at 11:26 AM
NeMewSys
(comments are locked)
|
//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!
Aug 17 '12 at 07:23 PM
NeMewSys
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
Aug 17 '12 at 07:38 PM
Bunny83
@NeMewSys, 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')
Aug 18 '12 at 09:21 PM
ScroodgeM
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... :(
Aug 19 '12 at 05:00 PM
NeMewSys
(comments are locked)
|
|
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------ --------------- is EXACTLY the same as having this in a "Example.js": ------Code------ --------------- 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).
Aug 18 '12 at 12:51 PM
Eric5h5
ah nice! Thanks! function test() { Gotta love this community :D
Aug 18 '12 at 01:53 PM
NeMewSys
(comments are locked)
|

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