- Home /
Enabled not working on Mac
Hey all, I'm currently developing on Windows, then checking in the project to our version control software to build on a Mac to deploy to iPad. A big problem I've got at the moment is that my .enabled function isn't working on Mac whilst it's perfectly fine on Windows.. can anyone explain why this is?
What I need to do is have a few variables that I can slot scripts in to on my game object, and then toggle those variables on/off to toggle the scripts in the slots on/off. Here's what I've got in the code:
var riddleTextScript1 : Component;
riddleTextScript1.enabled = false; //Or true, whichever
Answer by syclamoth · Nov 18, 2011 at 10:14 AM
If you look up Component in the Unity Documentation, you might be surprised to find that 'enabled' isn't even a member! The reason for this is that some components can be disabled, and others can't- which means that having 'enabled' be a member of the Component superclass could be a problem for components such as 'Transform' which cannot be disabled (and as such the 'enabled' boolean wouldn't make any sense).
The reason it was working for you on Windows is probably either that you were using a different version of Unity, or (as I think is more likely) the Javascript implementation there is a little more lax about this stuff. It's about this time that I usually complain about Javascript's fuzzy syntax, but at this point I think you know enough about that already.
The iPhone JavaScript compiler is very strict, and won't allow you to do this. Implicit downcasting is pretty dangerous as it is, and so it simply won't let you use 'enabled' when it can't know for sure that all objects which inherit from Component will even have that member.
If the script in question is a MonoBehaviour (i.e any script you write), you can use
var riddeTextScript1 : MonoBehaviour;
Which does have an 'enabled' member, and can be used in this way.
Excellent, thanks so much for this and for giving me the bit of education I needed as well ;D
Edit: Just to add to this; yes, it was indeed the Windows Javascript being 'a bit more lax' and the iOS version not letting me be lazy.
Right, or just use the base class which avtually implements the enabled property: Behaviour. Then you should be able to drag any kind of Behaviour onto your variable like: Animations, all kind of audio stuff, Camera, Light, NetworkView, Projector, $$anonymous$$onoBehaviours(aka scripts) and some more. Unfortunately there are a lot other Components that also have a enabled property but aren't derived from Behaviour: Colliders, Renderers, Rigidbody and some more.
I guess they want to keep the inheritance chain as short as possible since every derivation adds a very small overhead.
For more information on which classes is derived from what class see the runtime-classes-tree
Just to be clear: When you try to find the right type for your variable always search for a type that is as general as possible to be flexible but as concrete as necessary.
For example it makes no sense to use the more general type Renderer when your script actually expects a Skinned$$anonymous$$eshRenderer. It just allows the "user" of your script to make mistakes.
Answer by Grady · Nov 18, 2011 at 10:01 AM
Enabling and disabling scripts is a little bit more complicated then that, but you had the right idea!!!!!!!!!!!
All you would have to do is something like the code below:...
GetComponent(scriptNameHere).enabled = false;
This would disable a script that is attached to the same Game Object...
To disable a script on another Game Object, you would just do something like this:
GameObject.Find("gameObjectNameHere").GetComponent(scriptNameHere).enabled = false;
Just replace gameObjectNameHere
with the name of the other game object with the script that you are disabling!!!! And once again, add in the name of the script!
And then obviously you can just change .enabled = false
to true depending on what you're doing!!!! :)
You can also use tags to find the game object if you wish!
Also, I haven't tested these yet, so no 100% guarantees, but it should work!!!!
Here is the script reference for: GameObject.GetComponent
http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html if you wish
Hope this helps and feel free to comment back if you have any issues!
-Grady
I think they're looking for things that can be used from the inspector- GetComponent has to be hardcoded, and assumes that the component in question is on the same object.
Cheers for the suggestion, however I'm attaching the same script 4 times, so I guess using GetComponent(scriptNameHere) would be a problem since there'd be 4 of the same thing and I only want to access 1 of them at a time (ergo why I'm assigning the scripts as variables).