Search Unity

Snake game help.

Discussion in 'Getting Started' started by kmo86, Apr 14, 2021.

  1. kmo86

    kmo86

    Joined:
    Dec 15, 2020
    Posts:
    104
    I have completed a snake game that works fine in 2D and 3D. I now want to make it into a proper 3D game. I have the camera following the snakes head as a child of the head. I've left most of the scrips unchanged apart from the movement. Its working apart from when the snake eats food the snake doesn't grow. Snake segments spawn in the screen but dont move and are not joined onto the snake. I have an error come up saying:
    NullReferenceException: Object reference not set to an instance of an object
    GameManager.Grow () (at Assets/Scripts/GameManager.cs:27)
    Snake.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Snake.cs:61)
    And in my game manager script it highlight this line:
    segment.position = _segments[_segments.Count - 1].position;
     
  2. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    It's better if you use code tags for presenting your code, then the "experts" will be more inclined to help you.
     
  3. kmo86

    kmo86

    Joined:
    Dec 15, 2020
    Posts:
    104
    Ok here are all my scrips.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class GameManager : MonoBehaviour
    8. {
    9.     public TextMeshProUGUI gameOverText;
    10.     private List<Transform> _segments;
    11.     public Transform segmentPrefab;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         _segments = new List<Transform>();
    16.         _segments.Add(this.transform);
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.        
    23.     }
    24.  
    25.     public void Grow()
    26.     {
    27.         Transform segment = Instantiate(this.segmentPrefab);
    28.         segment.position = _segments[_segments.Count - 1].position;
    29.  
    30.         _segments.Add(segment);
    31.     }
    32.  
    33.     public void GameOver()
    34.         {
    35.             gameOverText.gameObject.SetActive(true);
    36.         }
    37.  
    38.     public void RestartGame()
    39.         {
    40.             SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    41.         }
    42. }
    43.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Food : MonoBehaviour
    6. {
    7.     public BoxCollider gridArea;
    8.  
    9.     private void Start()
    10.     {
    11.         RandomizePosition();
    12.     }
    13.     void RandomizePosition()
    14.     {
    15.         Bounds bounds = this.gridArea.bounds;
    16.  
    17.         float x = Random.Range(bounds.min.x, bounds.max.x);
    18.         float y = Random.Range(bounds.min.y, bounds.max.y);
    19.  
    20.         this.transform.position = new Vector3(Mathf.Round(x), Mathf.Round(y), 0.0f);
    21.     }
    22.  
    23.     private void OnTriggerEnter(Collider other)
    24.     {
    25.         if (other.tag == "Player")
    26.         {
    27.             RandomizePosition();
    28.         }
    29.     }
    30. }
    31.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Snake : MonoBehaviour
    6. {
    7.     private Vector2 _direction = Vector2.right;
    8.     private List<Transform> _segments;
    9.     public Transform segmentPrefab;
    10.     public int initialSize = 4;
    11.     private float horizontalInput;
    12.     private float verticalInput;
    13.     public float speed = 10;
    14.     public float turnSpeed = 20;
    15.     private float forwardInput;
    16.     private GameManager gameManager;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
    22.         _segments = new List<Transform>();
    23.         _segments.Add(this.transform);
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         transform.Translate(Vector3.forward * Time.deltaTime * speed);
    30.  
    31.         horizontalInput = Input.GetAxis("Horizontal");
    32.         transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed * horizontalInput);
    33.  
    34.         verticalInput = Input.GetAxis("Vertical");
    35.         transform.Rotate(Vector3.right * Time.deltaTime * turnSpeed * verticalInput);
    36.     }
    37.  
    38.  
    39.     private void FixedUpdate()
    40.     {
    41.         for (int i = _segments.Count - 1; i > 0; i--)
    42.         {
    43.             _segments[i].position = _segments[i - 1].position;
    44.         }
    45.  
    46.     }
    47.  
    48.  
    49.    
    50.  
    51.  
    52.     private void ResetState()
    53.     {
    54.        
    55.     }
    56.  
    57.     private void OnTriggerEnter(Collider other)
    58.     {
    59.         if (other.tag == "Food")
    60.         {
    61.            gameManager.Grow();
    62.         }
    63.         if (other.tag == ("Obstacle"))
    64.         {
    65.             gameManager.GameOver();
    66.             gameManager.RestartGame();
    67.         }
    68.     }
    69. }
    70.  
    I have only just copied the start code in the Game Manager from the snake script but it makes no difference.
     
  4. kmo86

    kmo86

    Joined:
    Dec 15, 2020
    Posts:
    104
    I have renamed the segment prefab to segment in the inspector and it doesn't give any error messages but still the segments spawn away from the snake and don't move
     
  5. kmo86

    kmo86

    Joined:
    Dec 15, 2020
    Posts:
    104
    Can anyone help?
     
  6. KazuoAkita

    KazuoAkita

    Joined:
    Jan 5, 2023
    Posts:
    1
    You should update Position each frame for each tail, write it in move function