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

What Makes Component Unabled to be Disabled?

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

  1. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    682
    I have a script attached to my player that controls movement. I've been struggling to disable it via script. I tried to disable it during runtime by unchecking the box in the inspector...however, it won't uncheck. Any idea what causes this?

    P.S. - It's not a part of being a Required Component.
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Are you getting any runtime error in the console? I have no idea, personally. It hasn't happened to me yet.
     
  3. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    682
    Nope. No errors whatsoever. It just won't uncheck. :(
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Are you enabling it in another script?
     
  5. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    682
    I don't enable it...per-say. What I do in another script (GameMaster) is Instantiate the Player Prefab (which the script I'm trying to disable is attached to).

    EDIT - But that's handled in the OnLevelLoaded() function.
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You need to show what exactly you are doing, otherwise, it is impossible to understand what is going on.
     
    Stoven likes this.
  7. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    682
    Here is the script that I'm trying to disable.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Movement : MonoBehaviour {
    5.  
    6.     public float rotateSpeed = 250f;
    7.     public float moveSpeed = 5f;
    8.  
    9.     private CharacterController controller;
    10.     private Transform myTransform;
    11.     private Animator anim;
    12.  
    13.     void Awake(){
    14.         controller = GetComponent<CharacterController>();
    15.         myTransform = transform;
    16.     }
    17.  
    18.     void Start () {
    19.         anim = GetComponent<Animator>();
    20.     }
    21.  
    22.     void Update () {
    23.         Turn();
    24.         Walk();
    25.     }
    26.  
    27.     private void Turn(){
    28.         if(Mathf.Abs(Input.GetAxis("Horizontal")) > 0){
    29.             myTransform.Rotate(0, Input.GetAxis("Horizontal") * Time.deltaTime * rotateSpeed, 0);
    30.             anim.SetFloat("Direction", Input.GetAxis("Horizontal"));
    31.         }else{
    32.             anim.SetFloat("Direction", 0);
    33.         }
    34.     }
    35.  
    36.     private void Walk(){
    37.         if(Mathf.Abs(Input.GetAxis("Vertical")) > 0){
    38.             controller.SimpleMove(myTransform.TransformDirection(Vector3.forward) * Input.GetAxis("Vertical") * moveSpeed);
    39.             anim.SetFloat("Speed", Input.GetAxis("Vertical"));
    40.         }else{
    41.             anim.SetFloat("Speed", 0);
    42.         }
    43.     }
    44.  
    45. }
    46.  
    In the inspector, during runtime you can click on the check box of a component on a GameObject to enable/disable it. I'm clicking the checkbox and it blinks, but it doesn't uncheck (disable).
     
  8. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    How does the code look where you disable it? Also, the enabling is important.
     
  9. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    i added to an object and i can disable it... try adding it to an empty gameObject and disabling it? maybe something else is enabling it
     
  10. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    682
    I don't think this is the problem, but here's where the player is Instantiated.

    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Runtime.Serialization.Formatters.Binary;
    6. using System.IO;
    7. using System.Text;
    8. using System.Runtime.Serialization;
    9. using System.Reflection;
    10. using System.Xml.Serialization;
    11.  
    12. public class GameMaster : MonoBehaviour{
    13.    
    14.     public Camera mainCamera;
    15.  
    16.     private GameObject playerCharacter;
    17.     private GameObject headsUpDisplay;
    18.     private GameObject eventSystem;
    19.     private GameObject pc;
    20.     private PlayerCharacter pcScript;
    21.     private Vector3 playerSpawnPointPos;
    22.  
    23.     void Awake(){
    24.         DontDestroyOnLoad(this);
    25.     }
    26.  
    27.     void OnLevelWasLoaded() {
    28.         if(Application.loadedLevelName != "MainMenu" && Application.loadedLevelName != "CharacterGeneration"){
    29.             playerSpawnPointPos = ChooseSpawnPoint.PickSpawnPoint(Application.loadedLevelName, PlayerPrefs.GetString("Last Zone", "Town"));
    30.             playerCharacter = (GameObject)Resources.Load("Prefabs/Player Character Prefab");
    31.             pc = Instantiate(playerCharacter, playerSpawnPointPos, Quaternion.identity) as GameObject;
    32.             pc.name = "Player Character";
    33.             pcScript = pc.GetComponent<PlayerCharacter>();
    34.             Instantiate(mainCamera, Vector3.zero, Quaternion.identity);
    35.             LoadCharacterData();
    36.             headsUpDisplay = (GameObject)Resources.Load("Prefabs/HUD Prefab");
    37.             eventSystem = (GameObject)Resources.Load("Prefabs/Event System Prefab");
    38.             Instantiate(headsUpDisplay, Vector3.zero, Quaternion.identity);
    39.             Instantiate(eventSystem, Vector3.zero, Quaternion.identity);
    40.             pcScript.LastZone = Application.loadedLevelName.ToString();
    41.             SaveCharacterData();
    42.         }
    43.     }
    44.  
    45.     public void SaveCharacterData(){
    46.         GameObject pc = GameObject.Find("Player Character");
    47.         PlayerCharacter pcClass = pc.GetComponent<PlayerCharacter>();
    48.        
    49.         PlayerPrefs.SetString("Player Name", pcClass.PlayerName);
    50.         PlayerPrefs.SetInt("Player Sex", (int)pcClass.PlayerSex);
    51.         PlayerPrefs.SetInt("Player Money", pcClass.PlayerMoney);
    52.         PlayerPrefs.SetString("Last Zone", pcClass.LastZone);
    53.     }
    54.  
    55. }
    56.  
     
  11. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Contrary to you, I think the disabling/enabling is exactly the problem :)

    That code doesn't help to understand what is going on. The enabling and disabling is definitely needed to help you further.
     
  12. shkar-noori

    shkar-noori

    Joined:
    Jun 10, 2013
    Posts:
    833
    You can't disable a component which doesn't have the Update or the FixedUpdate, OnEnable..etc functions used in the script.
     
  13. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    682
    Well I'm not (at this moment) referring to a script. I'm saying that MANUALLY (by clicking the check box) I can't disable the script during runtime.

    It does use the Update function.
     
    shkar-noori likes this.
  14. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    If you enable it by script, it makes perfect sense...
     
  15. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    682
    I FOUND IT!!!!!!!!!!!!!!!

    It's my HUD GameObject. It has a script attached to it. And in that script, it states that if the Menu (a child GameObject of the HUD) is active, to disable the Movement on the player, but if the Menu is NOT active, to enable the Movement on the player. So the problem is that as long as the Menu is NOT active, if I try to disable the Movement in any other way, the script on the HUD is just going to re-enable it right away.