- Home /
Respawnscript doesn't work as expected
So i'm trying to do a simple Doom-like FPS and i wanted to implement a respawn script.
using UnityEngine;
using System.Collections;
public class Respawn : MonoBehaviour
{
public static Transform[] respawnPoints;
public static Transform player;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
}
public static void respawnAt(int randomPoint)
{
player.position = respawnPoints[randomPoint].position;
}
}
This is what the respawn script looks like.
I want to activate the respawnAt method by entering a Zone
using UnityEngine;
using System.Collections;
public class FallOfMapZone : MonoBehaviour
{
void OnCollisionEnter(Collision col)
{
if(col.gameObject.transform.tag == "Player")
{
Respawn.respawnAt(Random.Range(1, 5));
}
}
}
But for some reason i get an error as soon as i go into the area: "NullReferenceExeption: Object Reference not set to an instance of an object. Respawn.respawnAt(Int32 randomPoint) (at Assets/Scripts/World/Respawn.cs:16)"
This has been answered many times, and the error is quite explicit... where do you assign your respawnPoints array, and how many elements does it have?
i asign the array at a spawnpointmanager which stores all the spawnpoint-transforms. It contains 5 elements
Answer by Josh1231 · Jan 10, 2015 at 03:18 PM
Don't make the them static, remove static from them then you find the gameobject and call that void, this is how you could do it:
GameObject respawnObj = GameObject.Find("Respawn");
Respawn respawn = respawnObj.GetComponent<Respawn>();
int randomSpawn = Random.Range(0,5);
respawn.respawnAt(randomSpawn);
Your answer
![](https://koobas.hobune.stream/wayback/20220613180045im_/https://answers.unity.com/themes/thub/images/avi.jpg)