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

Ending game menu appear issue

Discussion in 'Scripting' started by Niky220196, Jun 9, 2020.

  1. Niky220196

    Niky220196

    Joined:
    May 18, 2020
    Posts:
    3
    Hi, I'm trying to make a menu appear in the end of the game with restart button and the words "you won", but nothing appears. The menu should appear when the player reaches (position x = 170).


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class endingScript : MonoBehaviour
    6. {
    7.     public GameObject boy;
    8.  
    9.     public GameObject endMenu;
    10.  
    11.     void Update()
    12.     {
    13.        
    14.         if (boy.transform.position.x == 170)
    15.         {
    16.             Time.timeScale = 0;
    17.             endMenu.SetActive(true);
    18.         }
    19.     }
    20. }
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    the x position is a float, it is unlikely to ever be exactly 170. You would want to use a tolerance or maybe simply >= or <= depending on your game.
     
  3. Niky220196

    Niky220196

    Joined:
    May 18, 2020
    Posts:
    3
    Yes, it workes, thank you very much!