Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Bicko · Nov 18, 2011 at 09:49 AM · toggleactiveenabled

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
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Grady · Nov 18, 2011 at 10:35 AM 0
Share

true that!!!!!

avatar image Bicko · Nov 18, 2011 at 10:51 AM 0
Share

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.

avatar image Bunny83 · Nov 18, 2011 at 11:17 AM 1
Share

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.

avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Nov 18, 2011 at 10:16 AM 1
Share

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.

avatar image Bicko · Nov 18, 2011 at 10:52 AM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

What is the difference between these three ways of making an object disappear? 3 Answers

unity 2d trigger a script with a trigger? 0 Answers

Toggling my minimap 1 Answer

Please Help!!! Enable/Disable Script!!! 3 Answers

Disable Object Leave Script Running 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges