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

Bug Using .enabled = false on script component not working for all attached scripts

Discussion in 'Scripting' started by Joggla, Jul 20, 2023.

  1. Joggla

    Joggla

    Joined:
    Dec 2, 2019
    Posts:
    88
    Code (CSharp):
    1. private void AddAIActions()
    2.     {  
    3.         AIActionsBases.Add(this.AddComponent<GatherEnergyAI>());
    4.         AIActionsBases.Add(this.AddComponent<WanderingAI>());
    5.         foreach (AIActionBase aIActionBase in AIActionsBases)
    6.         {
    7.             aIActionBase.GetComponent<AIActionBase>().enabled = false;
    8.         }
    9.     }
    as you can see GatherEnergyAI is added first and will get disabled
    however WanderingAI is not changing to disabled...

    upload_2023-7-20_16-45-44.png

    however if i change the code around and put GatherEnergyAI second:


    Code (CSharp):
    1. private void AddAIActions()
    2.     {  
    3.         AIActionsBases.Add(this.AddComponent<WanderingAI>());
    4.         AIActionsBases.Add(this.AddComponent<GatherEnergyAI>());
    5.         foreach (AIActionBase aIActionBase in AIActionsBases)
    6.         {
    7.             aIActionBase.GetComponent<AIActionBase>().enabled = false;
    8.         }
    9.     }
    again... the first one added the .enabled works but for the second one it does not:
    upload_2023-7-20_16-47-17.png
     
  2. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    729
    Because you're calling GetComponent which just returns the first component attached to the object.

    Since you've already add them to the list, couldn't you do:

    aIActionBase.enabled = false;
     
    Joggla likes this.
  3. Joggla

    Joggla

    Joined:
    Dec 2, 2019
    Posts:
    88
    thank you so much
    this works:

    Code (CSharp):
    1. private void AddAIActions()
    2.     {  
    3.         AIActionsBases.Add(this.AddComponent<WanderingAI>());
    4.         AIActionsBases.Add(this.AddComponent<GatherEnergyAI>());
    5.         foreach (AIActionBase aIActionBase in AIActionsBases)
    6.         {
    7.             aIActionBase.enabled = false;
    8.         }
    9.     }
     
    flashframe likes this.
  4. Joggla

    Joggla

    Joined:
    Dec 2, 2019
    Posts:
    88
    could you pls help me with one more thing
    so i have this:

    Code (CSharp):
    1. public void ChooseAction()
    2.     {
    3.         AIActionsBases.FirstOrDefault().enabled = false;
    4.         AIActionsBases.OrderByDescending(x => x.score).ToList();
    5.         AIActionsBases.FirstOrDefault().enabled = true;
    6.     }
    so first i disable the first one in the list
    reshuffle them depending on score
    and reenable the first one after

    those 2 scripts should change in the list after score differs:
    upload_2023-7-20_17-27-4.png

    at the start it works , wandering is ontop of the list since score is higher
    upload_2023-7-20_17-27-44.png

    however this changes over time and the ai should now activate gather and disable wandering once score of gather is greater:
     

    Attached Files:

    Last edited: Jul 20, 2023
  5. Joggla

    Joggla

    Joined:
    Dec 2, 2019
    Posts:
    88
    upload_2023-7-20_17-29-8.png
    at this point the list should be swaping depending on the score but the list is unchanged and does not swap
     
  6. Joggla

    Joggla

    Joined:
    Dec 2, 2019
    Posts:
    88
    ok i fixed it.... :)
    just had to change this:
    Code (CSharp):
    1. AIActionsBases.OrderByDescending(x => x.score).ToList();
    to this:
    Code (CSharp):
    1. AIActionsBases = AIActionsBases.OrderByDescending(x => x.score).ToList();