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

GetComponent<>().enable Not Working!!!

Discussion in 'Scripting' started by DRRosen3, Dec 13, 2014.

  1. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    682
    I have two scripts attached to my player. One is Movement (self-explanitory). The other handles the player encountering enemies (called Encounters). The Encounters detects if an enemy is within the player's collider range and if so is supposed to stop the player from moving, and ask if the player wants to battle the enemy. However, it's not disabling the Movement script component.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Encounter : MonoBehaviour {
    6.  
    7.     private GameObject diagBox;
    8.     private GameObject diag;
    9.     private Text diagText;
    10.     private GameObject battlefield;
    11.     private GameObject playerOnePos;
    12.     private GameObject playerTwoPos;
    13.     private GameObject enemyTwoPos;
    14.     public GameObject enemy;
    15.  
    16.     void Start () {
    17.         diagBox = GameObject.Find("Dialogue Box");
    18.         diag = GameObject.Find("Dialogue");
    19.         diagText = diag.GetComponent<Text>();
    20.     }
    21.  
    22.     void Update () {
    23.    
    24.     }
    25.  
    26.     void OnTriggerEnter(Collider other){
    27.         if(other.gameObject.tag == "Enemy"){
    28.             enemy = other.gameObject;
    29.             StartCoroutine(Encounter(other.gameObject));
    30.         }
    31.     }
    32.  
    33.     private IEnumerator Encounter(GameObject encountered){
    34.         gameObject.GetComponent<Movement>().enabled = false; //This is the line that's supposed to disable the player's movement (Movement script).
    35.         diagBox.SetActive(true);
    36.         diag.SetActive(true);
    37.         while(!Input.GetKeyDown(KeyCode.Space)){
    38.             yield return null;
    39.         }
    40.         diagText.text = "Would you like to battle it?";
    41.         while(!Input.GetKeyDown(KeyCode.Y) && !Input.GetKeyDown(KeyCode.N)){
    42.             yield return null;
    43.         }
    44.         if(Input.GetKeyDown(KeyCode.Y)){
    45.             yield return StartCoroutine(SetUpBattle(gameObject, encountered));
    46.         }
    47.         if(Input.GetKeyDown(KeyCode.N)){
    48.             diagBox.SetActive(false);
    49.             diag.SetActive(false);
    50.            gameObject.GetComponent<Movement>().enabled = true; //Here is where I reactivate the player's movement if they choose not to battle.
    51.             yield return null;
    52.         }
    53.     }
    54.  
    55.     private IEnumerator SetUpBattle(GameObject player, GameObject opponent){
    56.         diagBox.SetActive(false);
    57.         diag.SetActive(false);
    58.         battlefield = (GameObject)Resources.Load("Prefabs/Battlefield Prefab");
    59.         Instantiate(battlefield, transform.position, transform.rotation);
    60.         playerOnePos = GameObject.Find("Player 1 Position");
    61.         playerTwoPos = GameObject.Find("Player 2 Position");
    62.         enemyTwoPos = GameObject.Find("Enemy 2 Position");
    63.         player.transform.position = playerOnePos.transform.position;
    64.         player.transform.rotation = playerOnePos.transform.rotation;
    65.         if(opponent.tag == "Enemy"){
    66.             opponent.transform.position = enemyTwoPos.transform.position;
    67.             opponent.transform.rotation = enemyTwoPos.transform.rotation;
    68.             opponent.GetComponentInChildren<EnemyAI>().enabled = false; //This line works perfectly! It get's the enemy's movement script and turns it off.
    69.         }else{
    70.             opponent.transform.position = playerTwoPos.transform.position;
    71.             opponent.transform.rotation = playerTwoPos.transform.rotation;
    72.         }
    73.         yield return null;
    74.     }
    75. }
    76.  
     
  2. Deleted User

    Deleted User

    Guest

    I suggest that you store that you set it movementComp as a variable and in start, set movementComp equal to

    gameObject.GetComponent<Movement>();

    Then in Encounter, you could say movementComp.enabled = false.

    (Also, in future, it is a good idea to use Unity Answers instead.)
     
  3. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    682
    I've tried storing it as a variable and it still doesn't disable the script.

    (I usually post here when I need a fast answer. With my luck (for some reason) responses to my questions on Unity Answers don't come for days...if ever.)
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Disabling a script doesn't disable everything. Try disabling it in the editor and see if it does as you intend. Mainly it will stop update and gui. Also, you can watch it in the editor while the game is running to see if it's disabled. Make sure there aren't any spelling errors. It's really easy to miss a capitalization difference.
     
  5. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Callbacks like OnCollision aren't disabled when the script is disabled. To disable those you need an early out check within the callback such as

    if (!enabled) return;
     
  6. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    682
    Hmmm. Okay. So at runtime I CAN'T disable the script. Clicking the checkbox just presses it, but it doesn't uncheck. Apparently I've got something else going on. x_x ...now to find the culprit.
     
  7. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    230
    I've had odd results with enabled in the past. So like hippocoder mentioned, I find it better to have your own variable named 'active' or something in the script.

    Then in the functions at the top you can put
    if(!active) return;

    Since the scripts are yours, you can easily manually stop them from running functions with a variable, rather than relying on unity to completely disable the whole script.
     
  8. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    682
    I'll keep that in mind, but I still can't uncheck it manually. Is it because the script uses Input.Get____ ?

    I ask because I'm now noticing ANOTHER script attached to the player also can't be unchecked. The only thing the two have in common, is that they use Input.Get_____ in their Update functions.
     
  9. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I can safely say that Input would never prevent a script from being disabled. Much more likely that something is enabling it or your code is broken.
     
    Stoven likes this.