- Home /
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....
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);
}
}
Thank you for the clarification on the "enabled" feature ValooFX!
Using a "isEnabled" boolean was the perfect solution! Thanks! :)
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.
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
Follow this Question
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