1
\$\begingroup\$

enter image description hereI wrote some code so when the player falls off a platform and collides with a trigger collider called Respawn Trigger, the player's transform position is set to an empty gameObject called Respawn Point. Everything seems to work well except I get the following error each time I collide with the trigger and respawn:

UnassignedReferenceException: The variable player of PlayerRespawn has not been assigned.You probably need to assign the player variable of the PlayerRespawn script in the inspector. PlayerRespawn.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/PlayerRespawn.cs:9)

This is my script I put on my Respawn Trigger gameObject:

using UnityEngine;

public class PlayerRespawn : MonoBehaviour {

    [SerializeField] private Transform player;
    [SerializeField] private Transform respawnPoint;

    void OnTriggerEnter2D(Collider2D other) {
        player.transform.position = respawnPoint.localPosition;
    }
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ And you did assign the player's transform in the editor to it? \$\endgroup\$
    – Zibelas
    Mar 11, 2023 at 7:24
  • \$\begingroup\$ Yes, I did. I just attached a gif file to the original post. \$\endgroup\$ Mar 11, 2023 at 16:41
  • 2
    \$\begingroup\$ Looks like you have more than one instance of this script in your scene — one configured correctly, one not. Try checking for null and logging the name of the object that's doing the check, or type PlayerRespawn into the search box at the top of your Hierarchy tab. \$\endgroup\$
    – DMGregory
    Mar 11, 2023 at 17:09

2 Answers 2

1
\$\begingroup\$

I was able to fix the issue by assigning it in code instead of in the inspector with this code:

public class PlayerRespawn : MonoBehaviour {

    private Transform player;
    private Transform respawnPoint;

    private void Start() {
        GameObject p = GameObject.Find("Player");
        player = p.transform;

        GameObject rp = GameObject.FindWithTag("Respawn Point");
        respawnPoint = rp.transform;
    }

    void OnTriggerEnter2D(Collider2D other) {
        player.position = respawnPoint.position;
    }
}

I am embarrassed to say I found the script also attached to the Player by mistake!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ As DMGregory wrote, the likely error is the script is somewhere else attached as well where it triggers too but is not assigned in the editor \$\endgroup\$
    – Zibelas
    Mar 11, 2023 at 20:05
  • 2
    \$\begingroup\$ There is nothing to be embarrassed about. Mistakes happen. If nobody made any, the would not need this site. Learning is all about doing a mistake once and remember it again the next time we encounter the same situation again. \$\endgroup\$
    – Zibelas
    Mar 12, 2023 at 8:08
  • \$\begingroup\$ This is very true...thank you, Zibelas!! \$\endgroup\$ Mar 12, 2023 at 15:02
0
\$\begingroup\$

The error message suggests that the "player" variable has not been assigned in the inspector of the PlayerRespawn script.

\$\endgroup\$
1
  • \$\begingroup\$ I did assign the player variable in the inspector through the PlayerRespawn script...very strange. \$\endgroup\$ Mar 11, 2023 at 17:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .