Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Pacman ghost script crashing unity (newbie C# help)

Discussion in 'Scripting' started by RandomNPC15, May 3, 2014.

  1. RandomNPC15

    RandomNPC15

    Joined:
    May 3, 2014
    Posts:
    3
    So I've been trying to learn to use Unity and C#, but I can't understand why this crashes. Can anyone find the glaringly obvious error and tell me what I'm doing wrong? I thought for sure this would work :(

    Code (csharp):
    1. public class Ghost : MonoBehaviour {
    2.  
    3.     public GameObject ThePac;
    4.     public float ghostSpeed = 1.5f;
    5.     private int XorY = 1;
    6.     private int colCount = 0;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         MoveY();
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.     }
    16.  
    17.     void MoveY()
    18.     {
    19.         Vector2 newPosition = transform.position;
    20.         XorY = 1;
    21.         if (ThePac.transform.position.y > transform.position.y)
    22.         {
    23.             while (colCount == 0)
    24.             {
    25.             newPosition.y += (1 * ghostSpeed * Time.deltaTime);
    26.             transform.position = newPosition;
    27.             }
    28.         }
    29.         if (ThePac.transform.position.y < transform.position.y)
    30.         {
    31.             while (colCount == 0)
    32.             {
    33.             newPosition.y += (-1 * ghostSpeed * Time.deltaTime);
    34.             transform.position = newPosition;
    35.             }
    36.         }
    37.     }
    38.  
    39.     void MoveX()
    40.     {
    41.         Vector2 newPosition = transform.position;
    42.         XorY = 0;
    43.         if (ThePac.transform.position.x > transform.position.x)
    44.         {
    45.             while (colCount == 0)
    46.             {
    47.             newPosition.x += (1 * ghostSpeed * Time.deltaTime);
    48.             transform.position = newPosition;
    49.             }
    50.         }
    51.         if (ThePac.transform.position.x < transform.position.x)
    52.         {
    53.             while (colCount == 0)
    54.             {
    55.             newPosition.x += (-1 * ghostSpeed * Time.deltaTime);
    56.             transform.position = newPosition;
    57.             }
    58.         }
    59.     }
    60.  
    61.     void OnCollisionEnter2D (Collision2D col)
    62.     {
    63.         colCount = 0;
    64.         if (XorY == 1)
    65.             MoveX ();
    66.         if (XorY == 0)
    67.             MoveY ();
    68.     }
    69.  
    70.     void OnCollisionExit2D (Collision2D col)
    71.     {
    72.         colCount = 1;
    73.     }
    74. }
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Just a quick guess, but i think it's because you're having loops in there which are never ending, at least this script doesn't allow them to.
     
  3. RandomNPC15

    RandomNPC15

    Joined:
    May 3, 2014
    Posts:
    3
    Hm, I'm not sure how to do this sort of behavior without looping though. Just to simplify things starting off, I was trying to make it to go along a y-axis path until it hits a wall, then go along a x-axis path until it hits a wall, etc.

    Guess I'll scrap this and try to think of something a little less infinite. Also, I have a feeling I should be using raycasts to change direction instead of waiting until I run into a wall.