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 Halo500 · Jan 02, 2016 at 10:29 AM · destroyswitchaddcomponentenableddisabled

C# - Disabled Script Still Runs!

I want to have a door switch disabled for a little while so mobile players won't spam and break the door, then have it enabled afterwards. I have disabled the script from another C# script, only to have the disabled script (box unchecked) still functioning....

alt text

I even took a different direction and destroyed the "vp_PlatformSwitch" script, then added it back in through "gameObject.AddComponent" but the newly added script would stop working altogether, not allowing me to press anything!

I was hoping if anybody could help me out with this little issue of mine. Is there a possible way to COMPLETELY disable the script or is there a better alternative. Here is the script:

 ///////////////////////////////////////////////////////////////////////////////////
 //    description:    This class will allow the player to interact with an object
 //                    in the world by input or by a trigger. The script takes a target
 //                    object and a message can be sent to that target object.
 //
 /////////////////////////////////////////////////////////////////////////////////
 
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class vp_Switch : vp_Interactable
 {
     
     public GameObject Target = null;
     public string TargetMessage = "";
     public AudioSource AudioSource = null;
     public Vector2 SwitchPitchRange = new Vector2(1.0f, 1.5f);
     public List<AudioClip> SwitchSounds = new List<AudioClip>();        // list of sounds to randomly play when switched
     
     
     protected override void Start()
     {
         
         base.Start();
         
         if(AudioSource == null)
             AudioSource = GetComponent<AudioSource>() == null ? gameObject.AddComponent<AudioSource>() : GetComponent<AudioSource>();
         
     }
     
     
     /// <summary>
     /// try to interact with this object
     /// </summary>
     public override bool TryInteract(vp_PlayerEventHandler player)
     {
         
         if(Target == null)
             return false;
         
         if(m_Player == null)
             m_Player = player;
         
         PlaySound();
         
         Target.SendMessage(TargetMessage, SendMessageOptions.DontRequireReceiver);
         
         return true;
         
     }
 
 
     /// <summary>
     /// 
     /// </summary>
     public virtual void PlaySound()
     {
         
         if(AudioSource == null)
             return;
         
         if( SwitchSounds.Count == 0 )
             return;
         
         AudioClip soundToPlay = SwitchSounds[ Random.Range( 0, SwitchSounds.Count ) ];
         
         if(soundToPlay == null)
             return;
         
         AudioSource.pitch = Random.Range(SwitchPitchRange.x, SwitchPitchRange.y);
         AudioSource.PlayOneShot( soundToPlay );
         
     }
     
     
     /// <summary>
     /// this is triggered when an object enters the collider and
     /// InteractType is set to trigger
     /// </summary>
     protected override void OnTriggerEnter(Collider col)
     {
         
         // only do something if the trigger is of type Trigger
         if (InteractType != vp_InteractType.Trigger)
             return;
 
         // see if the colliding object was a valid recipient
         foreach(string s in RecipientTags)
         {
             if(col.gameObject.tag == s)
                 goto isRecipient;
         }
         return;
         isRecipient:
 
         m_Player = col.transform.root.GetComponent<vp_PlayerEventHandler>();
 
         if (m_Player == null)
             return;
 
         if ((m_Player.Interactable.Get() != null) && (m_Player.Interactable.Get().GetComponent<Collider>() == col))
             return;
 
         // calls the TryInteract method which is hopefully on the inherited class
         TryInteract(m_Player);
         
     }
     
 }
 
switch.jpg (333.7 kB)
Comment
Add comment · Show 1
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 Halo500 · Jan 02, 2016 at 11:57 AM 0
Share

Thank you for the clarification on the "enabled" feature ValooFX!

Using a "isEnabled" boolean was the perfect solution! Thanks! :)

1 Reply

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

Answer by Polymo · Jan 02, 2016 at 10:33 AM

you could add an 'isEnabled' boolean which cancels the execution in TryInteract. Unity's script enabled feature only stops Unity messages like calling Update() and so on, it doesn't prevent you from still calling your methods.

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 Immanuel-Scholz · Jan 12, 2016 at 08:37 AM 0
Share

That, and also notice the description of OnTriggerEnter: "Trigger events will be sent to disabled $$anonymous$$onoBehaviours, to allow enabling Behaviours in response to collisions."

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

35 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnCollisionEnter still called when disabled, and destroy sets enabled to false. 1 Answer

Need help with Enabling/Disabling GUITextures with script 1 Answer

Button to create object??? 1 Answer

Something like Awake that runs even if disabled? 11 Answers

UI elements don't appear anymore after launching the game 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