Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Haikai · Dec 27, 2016 at 08:40 AM · javascriptcomponentenable and disable script

Script.Enabled does not work in Unity 5+

Hello! I've read all topics about it but somehow I cannot make it work.

How can I disable a script using javascript in Unity 5+?

ScriptName.enabled = false;

GetComponent("ScriptName").enabled = false;

GetComponent(ScriptName).enabled = false;

None of the above codes are working for me. Thanks if you can help.

Comment
Add comment · Show 2
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 KingFerretDino · Jan 19, 2017 at 03:28 PM 0
Share

have you tried using quotes aroud the script name

avatar image Eric5h5 KingFerretDino · Jan 19, 2017 at 05:42 PM 0
Share

You should not use quotes, and doing so has been deprecated in Unity now.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Eric5h5 · Dec 27, 2016 at 09:01 AM

GetComponent(ScriptName).enabled works fine, no different than other versions. Nothing changed regarding that. You have some other issue.

Comment
Add comment · Show 4 · 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 Haikai · Dec 27, 2016 at 04:44 PM 0
Share

It's possible, but I can't imagine what is happening. I tried the example Hellium said and all I get it:

"The name 'ScriptName' does not denote a valid type ('not found')."

Both scripts are attached at the same game object, no idea what should I try.

avatar image Eric5h5 Haikai · Dec 27, 2016 at 05:16 PM 0
Share

You wouldn't literally type "ScriptName" of course. But rather the name of the script.

avatar image Haikai Eric5h5 · Dec 27, 2016 at 09:08 PM 0
Share

Well yes, I was just explaining the situation. In the example bellow the name is "$$anonymous$$yscript.js". Anyway, thanks for trying.

Show more comments
avatar image
0

Answer by Hellium · Dec 27, 2016 at 09:09 AM

The documentation is quite self explanatory

https://docs.unity3d.com/ScriptReference/Behaviour-enabled.html

You surely do something wrong in your script. Make sure you are referencing the right script.

With these very simple scripts, everything works

 // MyScript.js
 
 var otherScript : Rotate;
 
 function Update () {
     if( Input.GetKeyDown( KeyCode.Space ))
         otherScript.enabled = !otherScript.enabled ;
 
     if( Input.GetKeyDown( KeyCode.A ))
         GetComponent.<Rotate>().enabled = !GetComponent.<Rotate>().enabled ;
 }


 // Rotate.js
 
 #pragma strict
 
 function Update () {
     transform.Rotate(Vector3.up * Time.deltaTime * 50, Space.World);
 }
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 Haikai · Dec 27, 2016 at 04:13 PM 0
Share

Not sure what is happening here but I tried your example and got at least 5 errors:

"The name 'Rotate' does not denote a valid type ('not found')."

avatar image Hellium Haikai · Dec 27, 2016 at 04:23 PM 0
Share

1st script is called $$anonymous$$yScript.js while the 2nd one is called Rotate.js

avatar image Haikai Hellium · Dec 27, 2016 at 04:25 PM 0
Share

I got it, I was speaking about not knowing what is going on with my Unity. Thank you very much for trying!

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

How do I disable Script on FP Controller? (Solved) 2 Answers

destroy hinge joint component from another object in javascript 2 Answers

Why am I getting this error ? 2 Answers

Activate a component when an animation clip in an animator is played? 0 Answers


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