Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Aviox1 · Sep 22, 2017 at 06:26 PM · unity 5crashmethod

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

143 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity randomly going unresponsive when importing script changes 0 Answers

iOS Build Crash : task_set_exception_ports(B07, 400, D03, 0, 0) Error 0 Answers

Game App Crashes when Application.Quit() 0 Answers

Unity closing automatically without any error? 1 Answer

Unity.exe caused an Access Violation (Unity 5.6) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges