Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Disabling/Enabling Scripts

Discussion in 'Editor & General Support' started by taragon, Dec 8, 2011.

  1. taragon

    taragon

    Joined:
    Jan 23, 2010
    Posts:
    119
    Through inventorymanager script, (the code that is attached) I am working on enabling/disabling 2 otherscripts, enemy damage and enemy police.
    I have tried both ways of checking to see if the condition is true (enabling scripts) and checking to see if the condition is false (disabling scripts). These scripts don't disable or enable.

    I have also set a debug for the condition to make sure it switches and also for whether the 2 other scripts (enemy damage and enemy police) are referenced correctly or not. Both of these work.

    I'm not sure what I'm doing wrong, but isn't this how you disable a script? (This is enabled true in the inspector)
    Code (csharp):
    1. function Update () {                                                       
    2.     if (keep_track_attack == false) {
    3.     enemy_damage_reference.enabled = false;
    4.     enemy_police_reference.enabled = false;
    5.     }
     
  2. Sycle

    Sycle

    Joined:
    Nov 24, 2009
    Posts:
    446
    No, that's not how you access scripts... (unless you've declared them as variables somewhere else in your script?)

    Normally you'd use GetComponent
     
  3. taragon

    taragon

    Joined:
    Jan 23, 2010
    Posts:
    119
    Enabled = true works; when the condition is met, both scripts, EnemyPoliceGuy and EnemyDamage are enabled. However, Enabled = false does not work; when the condition is false, enabled is still set to true. How can I fix this?

    Code (csharp):
    1. function Update () {                                                       
    2.     if (keep_track_attack == true) {
    3.     GameObject.FindWithTag("CopperEnable").GetComponent("EnemyPoliceGuy").enabled=true;
    4.     GameObject.FindWithTag("CopperEnable").GetComponent("EnemyDamage").enabled=true;
    5.     }
    6.     if (keep_track_attack == false) {
    7.     GameObject.FindWithTag("CopperEnable").GetComponent("EnemyPoliceGuy").enabled=false;
    8.     GameObject.FindWithTag("CopperEnable").GetComponent("EnemyDamage").enabled=false;  
    9.     }
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You don't need to check for both conditions, just do if/else.

    --Eric
     
  5. taragon

    taragon

    Joined:
    Jan 23, 2010
    Posts:
    119
    I did do the if/else, it's still not disabling the 2 scripts.