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 roamcel · Aug 16, 2011 at 09:44 AM · gameobjectremoveenabled

Gameobject script enable weird behavior

I have a rather simple gameobject with three scripts, amongst which a simple one with an OnMousedown() function, that performs correctly and prints to console.

I have the need to disable the simple script after three clicks, and I thought well this seems pretty straightforward so I came up with this:

 int maxclicksallowed = 3;
 int clicks = 0;

 void Update() {
   //do the update stuff
 }

 void OnMouseDown() {
 
   if (clicks > maxclicksallowed) {
     //this actually works: the script gets disabled in the inspector
     gameObject.GetComponent<thisscriptclass>().enabled = false;
     print("how dare you click so much, I'm deactivating this script");
 
   }
   else {
     //do the stuff i normally do
     //...
     clicks++;
   }
 }

Curious and unexpected thing is, despite the script being disabled in the inspector (the checkbox gets unflagged), mouse clicks on the object will still print the message, so WTF?

I don't like the idea of destroying the component script since it holds runtime state variables of the object that I'd need to save in some convolute way, and I don't like the idea of using a boolean condition, since the object will keep calling its update function "for nothing" (since it'll skip thanks to the false boolean condition mentioned).

So I'm ultimately wondering: what's the best way to 'disable' a script and prevent it's execution and Update calls until reactivation?

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

1 Reply

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

Answer by fredpointzero · Aug 16, 2011 at 10:06 AM

Just checkout the Monobehaviour reference at unity3d.com:

Note: The checkbox for disabling a MonoBehavior (on the editor) will only prevent Start(), Awake(), Update(), FixedUpdate(), and OnGUI() from executing. If none of these functions are present, the checkbox is not displayed.

OnMouseDown will still be executed on your GameObject.

You should deactiveate the entire GameObject (gameObject.active = false). If you want to separate components, just create a child to your game object, and add your component and disable the child on over click.

NB : you can use this to refer to gameObject.GetComponent<thisscriptclass>()

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 roamcel · Aug 16, 2011 at 10:43 AM 0
Share

You nailed it right on the head. Thanks.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Enabling/Disabling Multiple Tagged objects 1 Answer

Errors when trying to enable script on gameobject. 0 Answers

gameObject.GetComponent("Script").enabled = true not working 5 Answers

Acessing scripts in other game objects with prefabs, without using GameObject.Find 1 Answer

Snap object in game using script? 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