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
3
Question by peter · Dec 25, 2010 at 03:55 PM · javascriptdisableenable

Disable script from code

Hi all, How do I disable script and functions from that script within code.

I've used

 var script : thescript;
 thescript.enabled = false;

This results in the script being disabled but the functions still work. How do I disable or disconnect that script so that it completely turns off and does not affect the game?

Thanks all for any tips on this.

Peter

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

5 Replies

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

Answer by efge · Dec 25, 2010 at 04:23 PM

You should use the function GetComponent:

var script : thescript;

function Start() { script = GetComponent(thescript); script.enabled = false; ...

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 peter · Dec 26, 2010 at 04:47 AM 1
Share

Thx will try get component and report Cheers unity creators!

avatar image SwagGamez · May 23, 2015 at 04:57 PM 0
Share

I've tried using this method and it doesn't work for me. Am I missing something? I get a message saying "CarUserControl" isn't a valid type. Im sure its because of the colon (:) but why did it work in your case and not $$anonymous$$es?

Here is the script.

var script : CarUserControl;

function Start () { script = GetComponent("CarUserControl"); script.enabled = false; }

avatar image Bonfire-Boy · Mar 05, 2020 at 02:47 PM 0
Share

Since this has been resurrected yet again it is worth stressing that while this is the accepted answer, it does not do what the OP was asking for.

See CHPedersen's answer.

avatar image
5

Answer by CHPedersen · May 03, 2011 at 06:45 AM

Behavior.enabled, which MonoBehavior.enabled is inherited from, only switches the calling of Update() on/off, as was previously commented, and as per the documentation:

http://unity3d.com/support/documentation/ScriptReference/Behaviour-enabled.html?from=MonoBehaviour

Note that MonoBehavior is a class, of which an object is instantiated; you can't disconnect it entirely in the sense that it disables the ability to call those of its methods you yourself defined.

If you want to do that, you can either check to see whether the script is enabled before your code calls one of its methods yourself, or you can enclose the content of your methods inside the script in if(this.enabled) {}.

Comment
Add comment · 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
3

Answer by Berenger · Dec 25, 2010 at 04:11 PM

When you disable a component that, it' just stop doing the codein Update and stuff like that, but I think it doesn't disabled your own functions. I'm not sure your can do that, maybe try scrit.gameObject.active = false;

Comment
Add comment · Show 1 · 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 dottyfdsf · Jul 26, 2015 at 11:55 AM 0
Share

Yeah, that works fine, but it caused that the whole gameObject with all Components became false (inactive).

I've just resolved the problem! I wanted to disable script in door animation gameObject from another script (deactivator gameObject). So I wrote a code in deactivater gameObject in which I disable SphereCollider trigger (that is a collider on door gameObject), which means that the script in that door gameObject is also deactivated.

Here is the code:

using UnityEngine; using System.Collections; using UnityEngine.UI;

public class Trigger1 : $$anonymous$$onoBehaviour {

 public GameObject Player;
 public Text Text;
 //public GameObject doorLocked;
 public Collider collider;

 void Awake()
 {
     collider.GetComponent<SphereCollider>();
     collider.enabled = false;
 }

 void Start()
 {

     collider.enabled = false;
     Text.enabled = false;
     //doorLocked.SetActive (true);
 }
 
 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject == Player)
     {
         collider.enabled = true;
         Text.enabled = true;
         //doorLocked.SetActive (false);
     }

 }
 
 void OnTriggerExit(Collider other)
 {
     if (other.gameObject == Player) 
     {
         Text.enabled = false;
         //doorLocked.SetActive (false);
     }
 }

}

avatar image
0

Answer by graslany · May 13, 2013 at 04:21 PM

What about Destroy(this); ?

Here's the relevant doc link : http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html

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 Crazydadz · May 13, 2013 at 05:00 PM 0
Share

disable isnt the same as destroy...the answer is already solved by the way.

avatar image M0no · Jul 04, 2016 at 09:33 PM 0
Share

Destroy can be used, then you can "re-enable" the script with AddComponent(). However I'm not sure if this is very efficient

avatar image
-1

Answer by kami1339 · Mar 28, 2019 at 12:36 PM

use it

will works

 public GameObject otherobj;//your other object
     public string scr;// your secound script name
     void Start () {
     (otherobj. GetComponent(scr) as MonoBehaviour).enabled = false;
     }





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 Bunny83 · Mar 28, 2019 at 12:54 PM 1
Share

No, it does not for several reasons:

  • First of all this question is 9 years old and was about UnityScript, not C#

  • The GetComponent version that takes a string argument has been deprecated and is no longer documented so it shouldn't be used anymore. The main reason is that Unity now supports namespaces and you should always use either the generic version or the System.Type version.

  • You use an "as" cast without null check which is a no-go. If you know that the cast will always work, use a normal cast (which is faster and produces a better error if it actually fails). If you expect that the cast may fail you can use an as cast but you have to perform a null check on the casted reference. Since in your case the user can specify any string, chances are very high that either GetComponent returns null (because misspellt name) or the component is not a $$anonymous$$onoBehaviour and the as cast returns null.


If you want a generic way to disable any $$anonymous$$onoBehaviour, just declare your variable as $$anonymous$$onoBehaviour and drag&drop the component you want to disable onto the variable directly

 public $$anonymous$$onoBehaviour script;
 void Some$$anonymous$$ethod()
 {
     script.enable = false;
 }

If you don't know how to drag&drop a certain component, see my answer over here

avatar image Luca1047 Bunny83 · Mar 05, 2020 at 02:28 PM 0
Share

doesent work for me

Blockquote

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class DisabledScr2 : $$anonymous$$onoBehaviour {

 public $$anonymous$$onoBehaviour script;

 // Start is called before the first frame update
 void Start()
 {
     Debug.Log("e");
 }

 public void OnTriggerEnter2D(Collider2D node)
 {
     if (node.gameObject.tag == "Untagged")
     {
         Debug.Log("maboi youre breaking my heart");
         script.enable = false;
     }
 }

}

avatar image Bonfire-Boy Luca1047 · Mar 05, 2020 at 02:46 PM 0
Share

If you have a problem, you're best off posting a new question, explaining what the problem is, rather than adding it as a reply to an old comment that was only there to critique someone's hole-ridden answer to an even older question.

And remember, "doesn't work" doesn't tell anyone what your problem actually is.

And take note of CHPedersen's answer to this question. Disabling the $$anonymous$$onoBehaviour does not do what the OP was asking for - Bunny's code was not intended as an answer to the OP's question.

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

8 People are following this question.

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

Related Questions

Disable a script from another script 2 Answers

Disable SCRIPT HELP!!!! 1 Answer

What should I do? Disable the AI or have the AI simply be recylced and reused? 1 Answer

Disable all instances of a component 2 Answers

Re Enable C# script on camera 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