Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Update() being run on disabled scripts

Discussion in 'Scripting' started by RadioactiveChickenGames1, Sep 27, 2021.

  1. RadioactiveChickenGames1

    RadioactiveChickenGames1

    Joined:
    Sep 29, 2020
    Posts:
    15
    I'm making a game and collision detection is going kind of wild, so I'm trying to see if it would be better with a physics-based or a kinematic physics engine. I built scripts for both and attached them to the same object, thinking that I could get the same results by disabling one as only using one script. This turned out to be false, however, because the object behaves differently with just one script attached, with both scripts but only one enabled, or with both scripts enabled. If it helps, I can show my code, but there's a lot of it and there are 2 different scripts, so if it's not needed I won't show it.
    Thanks in advance for trying to help.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Update will not run on a disabled script. You can test this easily by putting a log statement inside the Update() method in the script and disabling it. It will not print.

    As for the rest of your problem it's very hard to help you without knowing any details or a specific problem you're having.
     
    Joe-Censored, Bunny83 and Brathnann like this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    I don't understand what you are asking. You don't seem to have a question in that paragraph above.
     
    PraetorBlue likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
  5. RadioactiveChickenGames1

    RadioactiveChickenGames1

    Joined:
    Sep 29, 2020
    Posts:
    15
    I'm asking if there's a way to get the scripts to run properly. Here's the code if it helps:
    Kinematic engine:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerBehaviour : MonoBehaviour
    6. {
    7.     //variables: input
    8.     public float ud;
    9.     public float rl;
    10.     //variables: physics
    11.     public Vector3 velocity;
    12.     public float walkForce;
    13.     public float jumpForce;
    14.     public Rigidbody rb;
    15.     public float terminalVelocity;
    16.     public bool decel;
    17.     //variables: random
    18.     public int health = 22;
    19.     public int jumps = 2;
    20.     public float dir = 1f;
    21.     public bool grounded;
    22.     public bool pounding;
    23.     public bool jumping;
    24.     //variables: communication
    25.     public GameObject sword;
    26.     public SwordBehaviour script;
    27.     public GameObject guiText;
    28.     public TextManipulator text;
    29.     //variables: dashing
    30.     public float dashSpeed;
    31.     public bool dashing;
    32.     public bool endDash;
    33.     public float dashTime;
    34.     public int maxAirDashes = 1;
    35.     public int airDashes = 1;
    36.  
    37.     // Start is called before the first frame update
    38.     void Start()
    39.     {
    40.         //initialize communications
    41.         rb = GetComponent<Rigidbody>();
    42.         script = sword.GetComponent<SwordBehaviour>();
    43.         text = guiText.GetComponent<TextManipulator>();
    44.     }
    45.    
    46.     //methods & IEnumerators
    47.     IEnumerator BeginDash()
    48.     {
    49.         airDashes--;
    50.         velocity.y = 0;
    51.         jumping = false;
    52.         yield return new WaitForSeconds(0);
    53.         script.sword.SetActive(false);
    54.         yield return new WaitForSeconds(dashTime);
    55.         if (!jumping)
    56.         {
    57.             dashing = false;
    58.         }
    59.         else
    60.         {
    61.             endDash = true;
    62.         }
    63.     }
    64.     public void BeginPound()
    65.     {
    66.         pounding = true;
    67.         rb.velocity = Vector3.zero;
    68.         velocity = Vector3.down * 25;
    69.     }
    70.  
    71.     // Update is called once per frame
    72.     void Update()
    73.     {
    74.         //input
    75.         ud = Input.GetAxis("Vertical");
    76.         rl = Input.GetAxis("Horizontal");
    77.         //walking
    78.         if (!pounding && rl > 0)
    79.         {
    80.             velocity.x += 5.92063160084f;
    81.             decel = false;
    82.         }
    83.         if (!pounding && rl < 0)
    84.         {
    85.             velocity.x -= 5.92063160084f;
    86.             decel = false;
    87.         }
    88.         if (Mathf.Abs(velocity.x) < 0.1)
    89.         {
    90.             velocity.x = 0;
    91.         }
    92.         if (velocity.x > 16)
    93.         {
    94.             velocity.x = 16;
    95.         }
    96.         if (velocity.x < -16)
    97.         {
    98.             velocity.x = -16;
    99.         }
    100.         if (Input.GetKeyUp(KeyCode.RightArrow) || Input.GetKeyUp(KeyCode.LeftArrow))
    101.         {
    102.             decel = true;
    103.         }
    104.         //jumping
    105.         if (Input.GetKeyDown(KeyCode.Z) && jumps > 0)
    106.         {
    107.             grounded = false;
    108.             jumping = true;
    109.             jumps--;
    110.             velocity.y = jumpForce + 9 * jumps;
    111.         }
    112.         //gravity
    113.         if (!grounded && !dashing || dashing && jumping && !grounded)
    114.         {
    115.             velocity.y -= 0.4f;
    116.         }
    117.         if (velocity.y < terminalVelocity)
    118.         {
    119.             velocity.y = terminalVelocity;
    120.         }
    121.         if (grounded)
    122.         {
    123.             velocity.y = 0;
    124.         }
    125.         //dashing
    126.         if (Input.GetKeyUp(KeyCode.C) && !dashing && airDashes > 0)
    127.         {
    128.             dashing = true;
    129.             StartCoroutine("BeginDash");
    130.         }
    131.         if  (rl > 0 && !dashing)
    132.         {
    133.             dir = 1f;
    134.         }
    135.         if (rl < 0 && !dashing)
    136.         {
    137.             dir = -1f;
    138.         }
    139.         if (dashing)
    140.         {
    141.             velocity.x = dir * dashSpeed;
    142.         }
    143.         //moving
    144.         transform.position += velocity * Time.deltaTime;
    145.         rb.velocity = Vector3.zero;
    146.         if (decel)
    147.         {
    148.             velocity.x *= 0.39685026299f;
    149.         }
    150.         if (!decel)
    151.         {
    152.             velocity.x *= 0.62996052494f;
    153.         }
    154.     }
    155.  
    156.     //all collisions
    157.     void OnCollisionEnter(Collision collision)
    158.     {
    159.         //damage
    160.         if (collision.gameObject.CompareTag("Hazard"))
    161.         {
    162.             health -= 3;
    163.             velocity.x = -96*(collision.transform.position.x - transform.position.x) / Mathf.Abs(collision.transform.position.x - transform.position.x);
    164.         }
    165.         //instant death
    166.         if (collision.gameObject.CompareTag("Death Hazard"))
    167.         {
    168.             health = 0;
    169.             Destroy(gameObject);
    170.         }
    171.         //ground resetting
    172.         if (collision.gameObject.CompareTag("Ground"))
    173.         {
    174.             jumps = 2;
    175.             grounded = true;
    176.             airDashes = maxAirDashes;
    177.             if (pounding)
    178.             {
    179.                 script.Dspawn();
    180.             }
    181.         }
    182.         //walls and cielings
    183.         if (collision.gameObject.CompareTag("LWall") && rl < 0)
    184.         {
    185.             velocity.x = 0;
    186.             Debug.Log("Collision with LWall");
    187.         }
    188.         if (collision.gameObject.CompareTag("RWall") && rl > 0)
    189.         {
    190.             velocity.x = 0;
    191.         }
    192.         if (collision.gameObject.CompareTag(" Cieling") && velocity.y != 0)
    193.         {
    194.             velocity.y = 0;
    195.         }
    196.         //coins
    197.         if (collision.gameObject.CompareTag("Money"))
    198.         {
    199.             Destroy(collision.gameObject);
    200.             text.AddMoney(1);
    201.         }
    202.     }
    203.     void OnCollisionStay(Collision collision)
    204.     {
    205.         //ground resetting
    206.         if (collision.gameObject.CompareTag("Ground"))
    207.         {
    208.             jumps = 2;
    209.             grounded = true;
    210.             pounding = false;
    211.             jumping = false;
    212.             if (!dashing)
    213.             {
    214.                 airDashes = maxAirDashes;
    215.             }
    216.             if (endDash)
    217.             {
    218.                 dashing = false;
    219.                 endDash = false;
    220.             }
    221.         }
    222.     }
    223.     void OnCollisionExit(Collision collision)
    224.     {
    225.         //leaving the ground
    226.         if (collision.gameObject.CompareTag("Ground"))
    227.         {
    228.             grounded = false;
    229.         }
    230.     }
    231. }
    Physics-based engine:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerPhysics : MonoBehaviour
    6. {
    7.     //variables: input
    8.     public float ud;
    9.     public float rl;
    10.     //variables: physics
    11.     public float walkForce;
    12.     public float jumpForce;
    13.     public Rigidbody rb;
    14.     public float terminalVelocity;
    15.     public Vector3 temp;
    16.     //variables: random
    17.     public int health = 22;
    18.     public int jumps = 2;
    19.     public float dir = 1f;
    20.     public bool grounded;
    21.     public bool pounding;
    22.     public bool jumping;
    23.     //variables: communication
    24.     public GameObject sword;
    25.     public SwordBehaviour script;
    26.     public GameObject guiText;
    27.     public TextManipulator text;
    28.     //variables: dashing
    29.     public float dashSpeed;
    30.     public bool dashing;
    31.     public bool endDash;
    32.     public float dashTime;
    33.     public int maxAirDashes = 1;
    34.     public int airDashes = 1;
    35.  
    36.     // Start is called before the first frame update
    37.     void Start()
    38.     {
    39.         //initialize communications
    40.         rb = GetComponent<Rigidbody>();
    41.         script = sword.GetComponent<SwordBehaviour>();
    42.         text = guiText.GetComponent<TextManipulator>();
    43.     }
    44.  
    45.     //methods & IEnumerators
    46.     IEnumerator BeginDash()
    47.     {
    48.         airDashes--;
    49.         temp.y = 0;
    50.         jumping = false;
    51.         yield return new WaitForSeconds(0);
    52.         script.sword.SetActive(false);
    53.         yield return new WaitForSeconds(dashTime);
    54.         if (!jumping)
    55.         {
    56.             dashing = false;
    57.         }
    58.         else
    59.         {
    60.             endDash = true;
    61.         }
    62.     }
    63.     public void BeginPound()
    64.     {
    65.         pounding = true;
    66.         temp = Vector3.down * 25;
    67.     }
    68.  
    69.     // Update is called once per frame
    70.     void Update()
    71.     {
    72.         //input
    73.         ud = Input.GetAxis("Vertical");
    74.         rl = Input.GetAxis("Horizontal");
    75.         //walking
    76.         if (!pounding && rl > 0)
    77.         {
    78.             rb.AddForce(walkForce * Vector3.right, ForceMode.Impulse);
    79.         }
    80.         if (!pounding && rl < 0)
    81.         {
    82.             rb.AddForce(walkForce * Vector3.left, ForceMode.Impulse);
    83.         }
    84.         if (rb.velocity.x > 16)
    85.         {
    86.             temp.x = 16;
    87.         }
    88.         if (rb.velocity.x < -16)
    89.         {
    90.             temp.x = -16;
    91.         }
    92.         //jumping
    93.         if (Input.GetKeyDown(KeyCode.Z) && jumps > 0)
    94.         {
    95.             grounded = false;
    96.             jumping = true;
    97.             jumps--;
    98.             rb.AddForce((jumpForce + 9 * jumps) * Vector3.up, ForceMode.Impulse);
    99.         }
    100.         //dashing
    101.         if (Input.GetKeyUp(KeyCode.C) && !dashing && airDashes > 0)
    102.         {
    103.             dashing = true;
    104.             StartCoroutine("BeginDash");
    105.         }
    106.         if (rl > 0 && !dashing)
    107.         {
    108.             dir = 1f;
    109.         }
    110.         if (rl < 0 && !dashing)
    111.         {
    112.             dir = -1f;
    113.         }
    114.         if (dashing)
    115.         {
    116.             temp.x = dir * dashSpeed;
    117.         }
    118.         //moving
    119.         rb.velocity = temp;
    120.     }
    121.  
    122.     //all collisions
    123.     void OnCollisionEnter(Collision collision)
    124.     {
    125.         //damage
    126.         if (collision.gameObject.CompareTag("Hazard"))
    127.         {
    128.             health -= 3;
    129.             rb.AddForce(-96 * Vector3.right * (collision.transform.position.x - transform.position.x) / Mathf.Abs(collision.transform.position.x - transform.position.x), ForceMode.Impulse);
    130.         }
    131.         //instant death
    132.         if (collision.gameObject.CompareTag("Death Hazard"))
    133.         {
    134.             health = 0;
    135.             Destroy(gameObject);
    136.         }
    137.         //ground resetting
    138.         if (collision.gameObject.CompareTag("Ground"))
    139.         {
    140.             jumps = 2;
    141.             grounded = true;
    142.             airDashes = maxAirDashes;
    143.             if (pounding)
    144.             {
    145.                 script.Dspawn();
    146.             }
    147.         }
    148.         //coins
    149.         if (collision.gameObject.CompareTag("Money"))
    150.         {
    151.             Destroy(collision.gameObject);
    152.             text.AddMoney(1);
    153.         }
    154.     }
    155.     void OnCollisionStay(Collision collision)
    156.     {
    157.         //ground resetting
    158.         if (collision.gameObject.CompareTag("Ground"))
    159.         {
    160.             jumps = 2;
    161.             grounded = true;
    162.             pounding = false;
    163.             jumping = false;
    164.             if (!dashing)
    165.             {
    166.                 airDashes = maxAirDashes;
    167.             }
    168.             if (endDash)
    169.             {
    170.                 dashing = false;
    171.                 endDash = false;
    172.             }
    173.         }
    174.     }
    175.     void OnCollisionExit(Collision collision)
    176.     {
    177.         //leaving the ground
    178.         if (collision.gameObject.CompareTag("Ground"))
    179.         {
    180.             grounded = false;
    181.         }
    182.     }
    183. }
    With both scripts enabled, the game runs really smoothly and the player goes right through walls.
    With just the first script enabled, jumping sends the player out of bounds and walking is really jittery.
    With only the first script on the object, none of these problems occur.
    Thanks for any consideration; that's a lot of code to dig through.
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Why do you want both scripts on the same object at the same time? As already mentioned, disabled scripts still get their collision methods called. If you really want to do it this way, in the collision methods first check if the component is enabled before doing anything.
     
    Bunny83 likes this.
  7. RadioactiveChickenGames1

    RadioactiveChickenGames1

    Joined:
    Sep 29, 2020
    Posts:
    15
    How is that done? I want them both so I can do a side-by-side comparison between them and see which one is better.
     
  8. trombonaut

    trombonaut

    Joined:
    Aug 11, 2021
    Posts:
    5
    Start your OnCollisionEnter with a check to see if the component is currently enabled. Skip the rest or return out of it if its not enabled. For example " if (!isActiveAndEnabled) return) " in the first line of your OnCollisionEnter.
     
  9. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    Kurt says about the collider callback thing and they are in your script, I've had this happen to me in the past prior to knowing they behave like this.
     
  10. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    232
    Make 2 objects next to each other. Each one having a different script.
    Both will be controlled together and youll be able to see the difference easily without the 2 scripts competing on the same object.