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;
It's better if you use code tags for presenting your code, then the "experts" will be more inclined to help you.
Ok here are all my scrips. Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; using UnityEngine.SceneManagement; public class GameManager : MonoBehaviour { public TextMeshProUGUI gameOverText; private List<Transform> _segments; public Transform segmentPrefab; // Start is called before the first frame update void Start() { _segments = new List<Transform>(); _segments.Add(this.transform); } // Update is called once per frame void Update() { } public void Grow() { Transform segment = Instantiate(this.segmentPrefab); segment.position = _segments[_segments.Count - 1].position; _segments.Add(segment); } public void GameOver() { gameOverText.gameObject.SetActive(true); } public void RestartGame() { SceneManager.LoadScene(SceneManager.GetActiveScene().name); } } Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class Food : MonoBehaviour { public BoxCollider gridArea; private void Start() { RandomizePosition(); } void RandomizePosition() { Bounds bounds = this.gridArea.bounds; float x = Random.Range(bounds.min.x, bounds.max.x); float y = Random.Range(bounds.min.y, bounds.max.y); this.transform.position = new Vector3(Mathf.Round(x), Mathf.Round(y), 0.0f); } private void OnTriggerEnter(Collider other) { if (other.tag == "Player") { RandomizePosition(); } } } Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class Snake : MonoBehaviour { private Vector2 _direction = Vector2.right; private List<Transform> _segments; public Transform segmentPrefab; public int initialSize = 4; private float horizontalInput; private float verticalInput; public float speed = 10; public float turnSpeed = 20; private float forwardInput; private GameManager gameManager; // Start is called before the first frame update void Start() { gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>(); _segments = new List<Transform>(); _segments.Add(this.transform); } // Update is called once per frame void Update() { transform.Translate(Vector3.forward * Time.deltaTime * speed); horizontalInput = Input.GetAxis("Horizontal"); transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed * horizontalInput); verticalInput = Input.GetAxis("Vertical"); transform.Rotate(Vector3.right * Time.deltaTime * turnSpeed * verticalInput); } private void FixedUpdate() { for (int i = _segments.Count - 1; i > 0; i--) { _segments[i].position = _segments[i - 1].position; } } private void ResetState() { } private void OnTriggerEnter(Collider other) { if (other.tag == "Food") { gameManager.Grow(); } if (other.tag == ("Obstacle")) { gameManager.GameOver(); gameManager.RestartGame(); } } } I have only just copied the start code in the Game Manager from the snake script but it makes no difference.
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