Search Unity

Script can't be disabled from another script.

Discussion in 'Scripting' started by denissuu, Feb 29, 2020.

  1. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162
    Hi there everyone! So, I'm currently making a weapon for my game, and when I shoot it and it hits the target, it should disable his patroling script, but it totally doesn't.Any other thing works fine, like let's say the animations or the navmeshagent work as intended and get changed/disabledd.

    I have tried different techniques like the following :

    Code (CSharp):
    1. // tried this
    2.  
    3. GetComponent<FOVDetection>().enabled = false;
    4.  
    5. // tried getting the game object before accesing this
    6. public GameObject killer;
    7.  
    8. Killer.GetComponent<FOVDetection>().enabled = false;
    9.  
    10. // also tried declaring a monobehaviour and disable that
    11.  
    12. public MonoBehaviour script;
    13.  
    14. void Start() {
    15.  
    16. script = GetComponent<FOVDetection>();
    17.  
    18. }
    19.  
    20. // and then trying to do this
    21.  
    22. script.enabled = false;
    23.  
    24. // Tried searching the gameObject and disable it like that
    25.  
    26. GameObject.Find("Killer").GetComponent<FOVDetection>().enabled = false;
    27.  
    28. // Also tried like this
    29.  
    30.      FOVDetection fovdetection = GetComponent<FOVDetection>();
    31.  
    32. // and then disabling it like this
    33.  
    34.      fovdetection.enabled = false;
    35.  
    36.  
    Let's just say I tried a bunch of ways.I also tried deleting the awake method in the FOVDetection script and keeping all of that together with the other stuff that was in start, that also didn't work.I've ran out of ideas and I needed to finish this so I could push my app update before the deadline that I've set for myself.

    Does anybody have any sort of idea why this is happening and how I could fix it?I'm desperate at this point as I can't understand what is going wrong in here.

    Thank you so much for the help!
     
  2. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    426
    Don't disable the script, just run the whole routine through an 'isActive' bool and switch it off that way. ;)
    Simple as hell, and works perfectly.
     
    denissuu likes this.
  3. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162

    I will try and do that tomorrow and come back with an update, thanks for the suggestion!
     
  4. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162

    I've found the issue, I have recently changed the script with another one, and forgot that I was using that instead of FOVDetection , so basically I was disabling a script that wasn't used haha.My mistake, thanks for the help!