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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Unity build isn't working well after game over

Discussion in 'Scripting' started by Vib3-Ch3cked, May 5, 2022.

  1. Vib3-Ch3cked

    Vib3-Ch3cked

    Joined:
    Jan 24, 2022
    Posts:
    7
    I made a snake-like game that functions pretty much fine until the player triggers their first game over. Once the game restarts, whenever the snake (player) collects items, they don't count as points and the snake doesn't gain another segment. The item respawns in a new location after coming in contact with it, but the segments and points aren't increasing. Dying by other means after the second game over doesn't solve this issue either.

    Mind you that this works perfectly in Unity, but these problems happen as a playable build

    For now I'll share my game manager, food, and enemy scripts. I also have a player and button (for menu) script

    GAME MANAGER
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class GameManager : MonoBehaviour
    9. {
    10.  
    11.     public int score;
    12.     public int scoreTarget;
    13.     public TMP_Text scoreText;
    14.     public TMP_Text winText;
    15.     public int points;
    16.     public GameObject Player;
    17.  
    18.     //***ABOVE IS FOR WIN***
    19.  
    20.     private void Start()
    21.     {
    22.         winText.enabled = false;
    23.         Time.timeScale = 1;
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         // If the esc key was pressed and released...
    30.         if (Input.GetKeyUp(KeyCode.Escape) == true)
    31.         {
    32.             // Quit the game (only works with built games)
    33.             Application.Quit();
    34.         }
    35.  
    36.         //****BOTTOM CODE RELOADS A SCENE AFTER WINNING****
    37.         if (Input.GetKeyUp(KeyCode.Space) == true)
    38.         {
    39.             // Reload the scene
    40.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    41.         }
    42.  
    43.         if (Player.GetComponent<Snake>().Lost == true)
    44.         {
    45.             Lose();
    46.         }
    47.     }
    48.  
    49.     /*public void ChangeScore()
    50.     {
    51.         scoreText.text = score.ToString();
    52.     }/*/
    53.  
    54.     //****USE FOR GAME OVER****
    55.     public void Lose()
    56.     {
    57.         Time.timeScale = 0;
    58.         winText.gameObject.SetActive(true);
    59.         winText.enabled = true;
    60.         winText.text = "You Lose\nPress Space to Restart";
    61.     }
    62. }
    63.  
    FOOD
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Food : MonoBehaviour
    6. {
    7.  
    8.     public BoxCollider2D gridArea;
    9.     public int points;
    10.  
    11.     [SerializeField] private AudioSource collectionSoundEffect;
    12.  
    13.     // Start is called before the first frame update
    14.     private void Start()
    15.     {
    16.         RandomizePosition();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.    
    23.     }
    24.  
    25.     private void RandomizePosition()
    26.     {
    27.         Bounds bounds = this.gridArea.bounds;
    28.  
    29.         float x = Random.Range(bounds.min.x, bounds.max.x);
    30.         float y = Random.Range(bounds.min.y, bounds.max.y);
    31.  
    32.         this.transform.position = new Vector3(Mathf.Round(x), Mathf.Round(y), 0.0f);
    33.     }
    34.  
    35.     private void OnTriggerEnter2D(Collider2D other)
    36.     {
    37.         if (other.tag == "Player")
    38.         {
    39.             collectionSoundEffect.Play();
    40.             RandomizePosition();
    41.         }
    42.     }
    43. }
    44.  
    ENEMY
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Enemy : MonoBehaviour
    6. {
    7.     public Transform player;
    8.     public float moveSpeed = 4f;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.    
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         Vector3 toPlayer = player.position - transform.position;
    20.         transform.position += toPlayer * moveSpeed * Time.deltaTime;
    21.         transform.rotation = Quaternion.LookRotation(Vector3.forward, toPlayer);
    22.     }
    23. }
    24.  
    Only other aspect to note is that whenever the player gains a new segment in Unity, this message pops up, but it hasn't caused any problems in-game upload_2022-5-5_15-44-41.png
     
    Last edited: May 5, 2022
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    You never show the code for your snake getting points or gaining a segment. However, the error is exactly your problem since it deals with prefabs and not instances. The Snake class is where your error is.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Put another way, where this error is happening, you are doing something to the prefab that is verboten, probably parenting.

    You can only do parenting with objects instantiate-d in the scene.
     
  4. Vib3-Ch3cked

    Vib3-Ch3cked

    Joined:
    Jan 24, 2022
    Posts:
    7
    Issue resolved, there was something wrong in my Grow function in the player script