SebLague/Ecosystem-2

ArgumentOutOfRangeException: Index was out of range

Nejcc opened this issue ยท 7 comments

Nejcc commented

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at :0)
System.ThrowHelper.ThrowArgumentOutOfRangeException () (at :0)
System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at :0)
Map.Remove (LivingEntity e, Coord coord) (at Assets/Scripts/Environment/Map.cs:138)
Map.Move (LivingEntity e, Coord fromCoord, Coord toCoord) (at Assets/Scripts/Environment/Map.cs:148)
Environment.RegisterMove (LivingEntity entity, Coord from, Coord to) (at Assets/Scripts/Environment/Environment.cs:63)
Animal.AnimateMove () (at Assets/Scripts/Behaviour/Animal.cs:221)
Animal.Update () (at Assets/Scripts/Behaviour/Animal.cs:76)

Nejcc commented

This error can be fix by adding material on fox and rabit prefab

lastElementIndex is -1 when map[regionX, regionY].Count is 0
This happened because the wrong coordinate was sent to the RegisterMove method.
In Animal.cs, change from:
Environment.RegisterMove (this, coord, moveTargetCoord);
to:
Environment.RegisterMove (this, moveFromCoord, moveTargetCoord);

@SebLague, do you accept pull requests?

@ericwerk I tried your fox, as well as adding the materials like @Nejcc said and unfortunately I am still getting the "Index was out of Range" error and my bunny ain't moving. Any other ideas?

Hello it's because there an error with material assignation, I don't know so much unity but object oriented programming and it seem that the material variable of plants, foxes or bunnies is inherited from the LivingEntity Class.
The error is raised here :
LivingEntity.cs:22

// Set material to the instance material
var meshRenderer = transform.GetComponentInChildren<MeshRenderer> ();
for (int i = 0; i < meshRenderer.sharedMaterials.Length; i++)
{
    if (meshRenderer.sharedMaterials[i] == material) {
        material = meshRenderer.materials[i];
        break;
    }
}

when meshRenderer.sharedMaterials[i] == material, material value is... none. I don't know what was the idea behind this. (still noob on unity framework)
So it's not a index error (or more accurately not directly), it's just that when the spawn living entity function try to spawn an animal, this one want to select a color for his material depending on his gender, material who is... at none value so it's raise an exception BUT it do not stop the simulation so we end up with an object not in the list in which it is expected.
The final error (index out of range) is raised when we try to move an animal, we take as index -> length of the list - 1 but the length is... 0 as the animal was not added in the list because of the previous error so we end up with a negative index.
I fix it by change meshRenderer.sharedMaterials[i] == material with meshRenderer.sharedMaterials[I] is Material.
I don't know if it's the original expected behavior but it work.
Oh and no problem with the the RegisterMove method, coord and moveFromCoord are always at the same value, don't know why two variables but I keep it with coord var because I don't look into all the simulation and don't want to break a thing with that.
At least plants and animals spawns without error but the simulation don't seem to start (or maybe it do but the screen stay grey in the game window), I will investigate that later for interested peoples.
The code seem cool and can be used as a base for interesting simulation.
Qwack qwak ! (sorry if my English is approximate, I'm a French duck)

OK found, the camera is just not set correctly and there seem to have no control on it implemented into the simulation but you just need to set it with these values :
position x:160 y:140 z:-69
rotation x:40 y:0 z:0
I can see the bunnies live^^

@CyberDuck79 i tried that its still not working (the material stuff)