Search Unity

Resolved Script isn't enabled but is still running??

Discussion in 'Scripting' started by jlorenzi, May 21, 2023.

  1. jlorenzi

    jlorenzi

    Joined:
    May 2, 2021
    Posts:
    292
    Edit: Yeah it was just a networking issue.

    So this is a confusing issue. I have a script for each class in my game. When a player selects a new class all the scripts for the classes get unenabled and then the one they select gets enabled.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SelectElement : MonoBehaviour
    6. {
    7.     public enum Element {fire, earth, water};
    8.     public Element element = new Element();
    9.     public Firebending fire;
    10.     public Earthbending earth;
    11.     public Waterbending water;
    12.     public RoundManager roundManager;
    13.  
    14.     void OnTriggerEnter(Collider other)
    15.     {
    16.         if ( other.CompareTag("Player") )
    17.         {
    18.             Debug.Log("selected element"); // works as expected
    19.             earth.enabled = false;
    20.             fire.enabled = false;
    21.             water.enabled = false;
    22.             switch ( element )
    23.             {
    24.                 case Element.fire:
    25.                     fire.enabled = true;
    26.                     break;
    27.                 case Element.earth:
    28.                     earth.enabled = true;
    29.                     break;
    30.                 case Element.water:
    31.                     water.enabled = true;
    32.                     break;
    33.             }
    34.  
    35.             roundManager.enabled = true;
    36.         }
    37.     }
    38. }
    39.  
    but even though this code seems to work (e.g. in the inspector the only script that's enabled is the one that the player selected), the last class you selected will still be active and you can still use the skills from it.

    I tried putting a log statement on one of the class scripts. It worked as it should, only running when the script was enabled, but the problem is the skills from that script were still activating.

    This is a multiplayer game so it could have something to do with that but other than that I got no ideas.
     
    Last edited: May 21, 2023
  2. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    602
    Enable state for MonoBehavior only affects Update,FixedUpdate,OnEnable,OnDisable and few other special methods called by the Unity. Otherwise enabled is just a boolean variable. If your own code is calling a method on disabled object the method will be called as usual, that's just basic behavior of most programming languages. In your custom methods it's up to you decide whether they should do anything while the object is disabled and check the disabled flag if necessary.

    If your script doesn't have any of the previously mentioned methods which are affected by enable state Unity won't even display the enable checkbox in the Inspector, because toggling it would have little effect. If your own logic manually checks enabled state it's not hard to add empty OnEnable to force the enable checkbox being display. Enable checkbox not being displayed in inspector doesn't affect your ability to toggle it from code.
     
    orionsyndrome and Bunny83 like this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    OnTriggerEnter (and other collision callbacks) still runs when the script is disabled.
     
    orionsyndrome likes this.
  4. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,696
    Enabled/Disabled takes the script out of the game loop. That means lifetime related methods (like Start()) and ones of the game loop itself like Update etc.
    Things that are triggered by something and thus happen "On"-something are not affected.
    If you want to disable a collision, disable the collider instead!
     
    orionsyndrome and Bunny83 like this.