hello, I am trying to make it so that when my player dies you press the R button and it respawns at the beginning position. I have tried the following code and whenever I die and try and respawn it comes up an error saying "The object of type 'GameObject' has already been destroyed but you are still trying to access it. How would I reword my code so that after the game object has been destroyed I can still respawn it? I have an object that is the player runs into it then it dies (destroy) thanks fr any help!
Here is my coding for each part that is being used
using UnityEngine;
using System.Collections;
public class Sawblade : MonoBehaviour {
public float speed = 300;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Rotate (Vector3.forward * speed * Time.deltaTime, Space.World);
}
void OnTriggerEnter(Collider c) {
if (c.tag == "Player") {
c.GetComponent().TakeDamage(10);
}
}
}
using UnityEngine;
using System.Collections;
public class Entity : MonoBehaviour {
public float health;
public void TakeDamage(float dmg){
health -= dmg;
if (health ();
SpawnPlayer (Vector3.zero);
}
private void SpawnPlayer(Vector3 spawnPos){
currentPlayer = Instantiate (player, spawnPos, Quaternion.identity) as GameObject;
cam.SetTarget(currentPlayer.transform);
}
private void Update(){
if (!currentPlayer) {
if (Input.GetButtonDown ("Respawn")) {
SpawnPlayer (Vector3.zero);
}
}
}
}
Trending Articles
More Pages to Explore .....