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
5
Question by luniac · Mar 06, 2014 at 07:22 PM · functionenabled

What exactly does script.enabled = false turn off

As the title suggests, if i use getcomponent(script).enabled = false, what exactly turns off.

Lets say i have Update() FixedUpdate() OnTriggerEnter() OnTriggerStay()

So upon disabling the script I've read that Update() gets turned off, but do OnTrigger Enter and OnTriggerStay still function?

The reason I'm asking is, I have intensive code running in update on a character. If that character dies I want that code to stop running BUT the dead character with a collider still exists so i have some Set() functions accessed from a second script on collision that affect some private dead characters variables.

If i disable the script upon death, would the Set() functions still function if i access them from a second script. The Set() functions are completely outside Update() or any loop for that matter.

Thanks.

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
15
Best Answer

Answer by robertbu · Mar 06, 2014 at 10:40 PM

According to the reference for 'Behaviour', the 'enable' flag impacts updating, so if I disable the flag, I would expect Update() and FixedUpdate() to stop being called, but all other functions to continue to be used. When I have questions like this, I usually just take a few minutes and test things out. Here is a script that tests the callbacks you ask about (and a couple of others).

 function Start() {
     InvokeRepeating("Repeating", 0,1);
     ACoroutine();
     enabled = false;
 }
 
 function Repeating() {
     Debug.Log("Repeating");
 }
 
 function ACoroutine() {
     while (true) {
         Debug.Log("Coroutine");
         yield WaitForSeconds(1.0);
     }
 }
 
 function Update () { 
     Debug.Log("Update");    
 } 
 
 function FixedUpdate() {
     Debug.Log("FixedUpdate");
 }
  
 function OnGUI() { 
     Debug.Log("GUI");
     GUI.Label(Rect(0,0,100,25), "GUI Label");
 } 
 
 function OnMouseDown() { 
     Debug.Log("Mouse down");
 } 
 
 function OnCollisionEnter() {
     Debug.Log("Collision");
 }
 
 function OnTriggerEnter() {
     Debug.Log("Trigger");
 }
 
 function PublicFunctionToCall() {
     Debug.Log("Public Function");
 }

Running it, I found the following still worked when the script was disabled:

  • Coroutines

  • InvokeRepeating() calls

  • OnCollisionEnter

  • OnTriggerEnter

  • OnMouseDown

  • Calls by other scripts

What stopped running:

  • Update

  • FixedUpdate

  • OnGUI

Comment
Add comment · Show 6 · 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 luniac · Mar 07, 2014 at 02:11 AM 0
Share

Awesome answer bro, thanks.

avatar image fafase · Mar 07, 2014 at 05:50 AM 0
Share

??!! That is against anything you would expect to be logical!! Not that the answer is wrong but disabling a script I would expect it to do nothing, but it seems most of the event callbacks are in use... good to know but I consider this a wrong implementation from Unity.

avatar image luniac · Mar 07, 2014 at 01:19 PM 0
Share

yup it is weird and dangerous in some ways.

avatar image stevethorne · Mar 07, 2014 at 10:41 PM 1
Share

I believe that on top of this, Start won't be called on a script that is not enabled, but Awake will.

avatar image Eric5h5 · Mar 07, 2014 at 11:02 PM 1
Share

It's intentional behavior, so you can make scripts "sleep" by turning off Update, but have the option to wake themselves up in OnTriggerEnter or whatever. I think the the property name is misleading though...ins$$anonymous$$d of ".enabled", something like ".updatesEnabled" would probably be better.

Show more comments
avatar image
2

Answer by sirdudalot · Oct 17, 2016 at 07:30 PM

robertbu nailed it, but I wanted to add that I just put a little...

if (!enabled) return;

... at the beginning of the trigger and collision functions when I want them to also be disabled.

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 Schwartz_M · Jun 28, 2018 at 05:07 PM 0
Share

Thank you! This is such a simple solution to some of the problems I've been encountering.

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

24 People are following this question.

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

Related Questions

onMouseDown call function from other script 1 Answer

Making a var appear on click 1 Answer

Function triggered when scene changes? 4 Answers

gameObject.tag doesn't work as I'd expect inside a function Update 0 Answers

How do i run scripts more times 2 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