- Home /
 
MoveCreatures() Method crashes unity HELP
void MoveCreatures() { for (int x = 0; x < mapWidth; x++) { for (int y = 0; y < mapHeight; y++) { Move(GetLocationToMoveTo(), map[x, y]); } } }
void Move(byte direction, Creature creature) {
     //direction
     //6  7  8
     //4     5
     //1  2  3
     int newX, newY;
     //finds new location
     switch (direction)
     {
         case 1:
             newX = creature.GetX() - 1;
             newY = creature.GetY() - 1;
             break;
         case 2:
             newX = creature.GetX();
             newY = creature.GetY() - 1;
             break;
         case 3:
             newX = creature.GetX() + 1;
             newY = creature.GetY() - 1;
             break;
         case 4:
             newX = creature.GetX() - 1;
             newY = creature.GetY();
             break;
         case 5:
             newX = creature.GetX() + 1;
             newY = creature.GetY();
             break;
         case 6:
             newX = creature.GetX() - 1;
             newY = creature.GetY() + 1;
             break;
         case 7:
             newX = creature.GetX();
             newY = creature.GetY() + 1;
             break;
         case 8:
             newX = creature.GetX() + 1;
             newY = creature.GetY() + 1;
             break;
         default:
             newX = creature.GetX() + 1;
             newY = creature.GetY() + 1;
             break;
     }
     if(newX >= 0 && newX <= mapWidth - 1 && newY >= 0 && newY <= mapHeight - 1)//shows that new loaction is valid
     {
         if(creature.GetBrand() != 0)//Empty spaces do not move
         {
             if(creature.GetBrand() == 1 && map[newX,newY].GetBrand() == 2)//prey to predator
             {
                 creature.SetEqualTo(emptyTemplate);
                 map[newX, newY].Reproduce(map,mapWidth,mapHeight);
             }
             else if (creature.GetBrand() == 2 && map[newX, newY].GetBrand() == 1)//predator to prey
             {
                 map[newX, newY].SetEqualTo(creature);
                 creature.SetEqualTo(emptyTemplate);
                 map[newX, newY].Reproduce(map, mapWidth, mapHeight);
             }
             else if ((creature.GetBrand() == 1 || creature.GetBrand() == 2) && map[newX, newY].GetBrand() == 0)//just moving to empty space
             {
                 map[newX, newY].SetEqualTo(creature);
                 creature.SetEqualTo(emptyTemplate);
             }
             //prey to prey and predator to predator would not move so they dont need code to move
         }
     }//this actually moves them
 }
 
               byte GetLocationToMoveTo() { //1-9 at keypad locations with 5 being the creatures original location //returns byte between 1-8 //678 //4 5 //123 return (byte)Random.Range(1, 9); }
//Creature class just for reference
public class Creature { private int maxHealth; private int health; private byte type;//one of three states| 0 = empty| 1 = Prey| 2 = Predator| private int x; private int y; private Color color;
 public Creature(byte type, int maxHealth)
 {
     this.type = type;
     this.health = maxHealth;
     this.maxHealth = maxHealth;
     this.SetColor();
 }
 public void Reproduce(Creature[,] creature, int mapWidth, int mapHeight)//also calls resets health
 {
     this.ResetHealth();
     for(int x = this.x - 1; x <= this.x + 1; x++)
     {
           for (int y = this.y - 1; x <= this.y + 1; y++)
           {
                 if (x > 0 && x < mapWidth - 1 && y > 0 && y < mapHeight - 1)
                 {
                     if (creature[x, y].GetBrand() == 0)
                     {
                         creature[x, y].SetEqualTo(this);
                     }
                 }
           }
     }
 }//Done
 public void ResetHealth()
 {
     this.health = this.maxHealth;
 }
 private void SetColor()
 {
     if(this.type == 1)
     {
         this.color = Color.green;
     }
     else if(this.type == 2)
     {
         this.color = Color.red;
     }
 }
 public Color GetColor()
 {
     return this.color;
 }
 public byte GetBrand()
 {
     return this.type;
 }
 public void SetBrand(byte type)
 {
     this.type = type;
     if(type == 1)
     {
         this.color = Color.green;
     }
     else if(type == 2)
     {
         this.color = Color.red;
     }
 }
 public int GetX()
 {
     return this.x;
 }
 public int GetY()
 {
     return this.y;
 }
 public void SetX(int x)
 {
     this.x = x;
 }
 public void SetY(int y)
 {
     this.y = x;
 }
 public void SetHealth(int health)
 {
     this.health = health;
 }
 public int GetMaxHealth()
 {
     return this.maxHealth;
 }
 public int GetCurrentHealth()
 {
     return this.health;
 }
 public void SetEqualTo(Creature creature)
 {
     this.color = creature.GetColor();
     this.maxHealth = creature.GetMaxHealth();
     this.health = creature.GetCurrentHealth();
     this.type = creature.GetBrand();
 }
 
               }
               Comment
              
 
               
              Your answer