Search Unity

Bug why is this message appearing only once.

Discussion in 'Scripting' started by sahilsharma220202004, Jul 24, 2021.

  1. sahilsharma220202004

    sahilsharma220202004

    Joined:
    Nov 18, 2020
    Posts:
    33
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     private Vector3 direction = Vector2.right;
    9.     private float moveTimeCount;
    10.     [SerializeField] private float moveAfterTime;
    11.     [SerializeField] Transform snakeBodyPart;
    12.     private List<Transform> snakeBody;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         snakeBody = new List<Transform>();
    18.         snakeBody.Add(this.transform);
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if (direction != Vector3.down)
    25.         {
    26.             if (Input.GetKeyDown(KeyCode.W))
    27.             {
    28.                 direction = Vector2.up;
    29.             }
    30.         }
    31.         if (direction != Vector3.up)
    32.         {
    33.             if (Input.GetKeyDown(KeyCode.S))
    34.             {
    35.                 direction = Vector2.down;
    36.             }
    37.         }
    38.         if (direction != Vector3.right)
    39.         {
    40.             if (Input.GetKeyDown(KeyCode.A))
    41.             {
    42.                 direction = Vector2.left;
    43.             }
    44.         }
    45.         if (direction != Vector3.left)
    46.         {
    47.             if (Input.GetKeyDown(KeyCode.D))
    48.             {
    49.                 direction = Vector2.right;
    50.             }
    51.         }
    52.     }
    53.  
    54.     private void FixedUpdate()
    55.     {
    56.         moveTimeCount += Time.fixedDeltaTime;
    57.         if (moveTimeCount > moveAfterTime)
    58.         {
    59.             for (int i = snakeBody.Count - 1; i > 0; i--)
    60.             {
    61.                 snakeBody[i].position = snakeBody[i - 1].position;
    62.             }
    63.             transform.position += direction;
    64.             moveTimeCount = 0;
    65.         }
    66.     }
    67.  
    68.     void Grow()
    69.     {
    70.         Vector2 bodyPartPos = snakeBody[snakeBody.Count - 1].position;
    71.         Transform snakePartNew = Instantiate(snakeBodyPart, bodyPartPos, snakeBodyPart.rotation);
    72.         snakeBody.Add(snakePartNew);
    73.         snakePartNew.tag = "Body";
    74.     }
    75.  
    76.     void RestartGame()
    77.     {
    78.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    79.     }
    80.  
    81.     private void OnTriggerEnter2D(Collider2D collision)
    82.     {
    83.         if (collision.gameObject.CompareTag("Food"))
    84.             Grow();
    85.         else if (collision.gameObject.CompareTag("Wall"))
    86.             RestartGame();
    87.         else if (collision.gameObject.CompareTag("Body"))
    88.             Debug.Log("Quit");
    89.     }
    90. }
     
  2. sahilsharma220202004

    sahilsharma220202004

    Joined:
    Nov 18, 2020
    Posts:
    33
    I am making snake replica in which I am making functionality about loosing game when you touch/eat yourself. When your head collides with your body we get a Quit message, but it is appearing no matter what I do for the 1st time my snake eats food.
    Note: Snake head has tag Player and snake body is a prefab with no tag
     
  3. ErinZ

    ErinZ

    Joined:
    Aug 13, 2019
    Posts:
    4
    Its possible when you grow you are colliding with your newly instantiated body part. Try adding a bufferzone to your bodyparts?
     
  4. sahilsharma220202004

    sahilsharma220202004

    Joined:
    Nov 18, 2020
    Posts:
    33
    Actually, I have made this game such that when snake eats food it grows(new body prefab is spawned which follows the snake like body) and that body gets a tag-"Body", so that later I can check in OnTriggerEnter2D whether I have collided with my own body. It's working nicely but is not working for only the 1st time snake eats food, I think it is something related to my Grow method.
    Also, I am a beginner .So let's search about bufferzone also;D
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Are you saying you expect more messages than one message? Is it possible you have the "Collapse" button selected in your log? That will collapse all identical messages to one line.

    Beyond that, I like that you already have some Debug.Log() calls in... the next trick to learn what your code is actually doing is to add way more of them.

    In fact, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  6. sahilsharma220202004

    sahilsharma220202004

    Joined:
    Nov 18, 2020
    Posts:
    33
    Ok I will try that.
    No, I want Quit message only when snake touches his own body, but it's appearing once snake eats food 1st time.
    Actually what I want from game is that when snake eats food he must grow(add a body prefab that follows him) and then that untagged body prefab gets the tag "Body" so that when my snake touches his own body I can check in OnTriggerEnter2D if the other object tag is body

    Edit: I got it!! The reason I am getting Quit 1st time I eat food is because if we check how our snake moves, we see that new snake body prefab gets position of the previous body prefab, but the 1st body prefab gets position of head, and head has script attached for when it enters a trigger Quit is shown, while in other cases body is sent to position of last body prefab.
    So happy that I am learning.
     
    Last edited: Jul 25, 2021
    Kurt-Dekker likes this.