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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

C# Respawn Script Not Working

Discussion in '2D' started by Cobble, Nov 6, 2016.

  1. Cobble

    Cobble

    Joined:
    May 9, 2015
    Posts:
    33
    Hi! I made a simple respawn script, but the player doesn't respawn at the point I want her to, and I definitely typed in the right coordinates. I appreciate any help!

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Respawn : MonoBehaviour {
    5.  
    6. public Vector2 spawnPoint;
    7.  
    8. void OnTriggerEnter2D(Collider2D other) {
    9.  
    10. if (other.gameObject.tag == "Enemy")
    11. {
    12. Debug.LogError("Collision detected, Trying to Respawn");
    13. transform.position = new Vector2();
    14. }
    15. }
    16. }
     
  2. albertvasconcellos

    albertvasconcellos

    Joined:
    Feb 18, 2016
    Posts:
    5
    In your OnTriggerEnter2D function, you need to change the transform.position to equal the spawn point that you defined earlier.

    For example:

    transform.position = spawnPoint;
     
  3. Cobble

    Cobble

    Joined:
    May 9, 2015
    Posts:
    33
    I feel stupid now. Thank you!