Search Unity

Question Cannot use for loop when AddObservation()

Discussion in 'ML-Agents' started by KevinOfCathay, Jun 21, 2020.

  1. KevinOfCathay

    KevinOfCathay

    Joined:
    Dec 21, 2019
    Posts:
    7
    I found a weird behavior when I tried to add observations using a for loop or foreach loop, for example
    Code (CSharp):
    1. foreach(var data in list_float){
    2.     sensor.AddObservation(data);
    3. }
    I will get an error says "sensor size must match". But the number of floats in my list does match my sensor's size. However, if I add observation one by one like this:
    Code (CSharp):
    1. sensor.AddObservation(list_float[0]);
    2. sensor.AddObservation(list_float[1]);
    3. sensor.AddObservation(list_float[2]);
    4. ...
    Then everything works fine.
    I'm not sure if this is a bug, or I did something wrong.
     
  2. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473
    Did you double-check list_float is the correct size? Btw, I think you can add IEnumerables directly, try sensor.AddObservation(list_float)
     
  3. KevinOfCathay

    KevinOfCathay

    Joined:
    Dec 21, 2019
    Posts:
    7
    Thanks for the reply.
    I tried foreach again and somehow this time it worked. So my guess is there was a mistake in my code elsewhere, when unity reach that broken code, it stops executing the remaining part of the code, which causes my list not get initialized (and therefore get an empty list).
     
  4. celion_unity

    celion_unity

    Joined:
    Jun 12, 2019
    Posts:
    289
    Glad you got it working.

    It's hard to tell without the exact text of the error, but "sensor size must match" was probably due to Agents with the same Behavior Name and different observation sizes.