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. Dismiss Notice

Unity restart function

Discussion in 'Getting Started' started by Yegor42069, Feb 21, 2023.

  1. Yegor42069

    Yegor42069

    Joined:
    Dec 5, 2022
    Posts:
    26
    Im making a restart function but nothing is working ive been looking at tutorials on how to make it work but its just not doing anything when my player lands in the lava he can still move and do every thing here is my code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PlayerLife : MonoBehaviour
    7. {
    8.     public GameObject player;
    9.     public Transform respawnPoint;
    10.     void Start()
    11.     {
    12.  
    13.     }
    14.     void Update()
    15.     {
    16.  
    17.     }
    18.  
    19.  
    20.     void OnTriggerEnter2d(Collider2D other)
    21.     {
    22.         if (other.gameObject.CompareTag("Player"))
    23.         {
    24.             player.transform.position = respawnPoint.position;
    25.         }
    26.     }
    27. }
    28.    
    29.  
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,863
  3. Yegor42069

    Yegor42069

    Joined:
    Dec 5, 2022
    Posts:
    26
    Thank you i got it fixed
     
  4. TylerJay-B

    TylerJay-B

    Joined:
    Dec 29, 2018
    Posts:
    17
    You should also avoid comparing strings when checking if the collision was your player

    Code (CSharp):
    1. if (other.TryGetComponent(out Player player))
    2. {
    3.        // is the player
    4.        other.transform.position = respawnPoint.position;
    5. }