Remove/add sensors before training through code

I'm using 4 different types of sensors with my agents. I would like to test some combinations of using/not-using those sensors. For example:
| S1 | S2 | S3 | S4
comb1 | X | X | X | X
comb2 | X | | X |
comb3 | | X | X | X
, where combination 1 uses the 4 sensors, the 2nd one uses only first and third sensors and combination 3 uses sensors 2, 3 and 4.
So, to make testing easier i implement the following code

public class SensorManager : Monobehaviour {
    public DictionarySensorBool sensors;

    void Awake() {
        foreach (SensorComponent sensor in sensors.Keys) {
            bool useIt = sensors[sensor];
            if (!useIt) {

This script removes the unchecked sensors from the agent (the agent have this sensors attached and the dictionary keys are references to those sensors).
However, i ran the training from jupyter notebook to check if the sensors where removed and it doesn't. I'm still getting observations from the sensors even when they where removed from the agent object. So,

  • How could i remove the sensors (or add the ones i want) through code and not get their observations.
  • Which other mechanism could i use to avoid remove/add the sensors manually, or having an agent for each posible combination and enable/disable them.

Thanks in advance, sorry for my bad english.

This might simply be a timing issue, where ML-Agents initialization code runs prior to your code that's removing sensors. Take a look at script execution order settings:

1 Like

Thanks and yes, that might be one of the problems, however, i realised that the main problem was that the Destroy function destroys the object at the end of the frame. So, a solution could be to use DestroyInmediate instead. But, it is not recommended to use it. Another solution could be to add the sensors copying them from a prefab with this code instead of removing them. I post this in case anyone has the same problem.