So my game is about getting to the exit before a wall of spikes kills you. My problem is that the wall won’t disappear when a player gets killed by something that’s not the wall. There’s a video to show whats happening. What is supposed to happen is when the player hits the wall the player will die and so will the wall. The wall will respawn and so will the wall to there original positions. Working fine. Its just I want the wall to disappear along with the player when the player gets killed by something that is not the wall. But when that happens the whole thing starts messing up. I have no idea what to do by this point.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class KillScript : MonoBehaviour {
public AudioClip deathSound;
AudioSource _Audio;
public LevelManager levelManager;
private GameObject clone;
void Start () {
levelManager = FindObjectOfType<LevelManager>();
_Audio = GetComponent<AudioSource>();
clone = GameObject.Find("Wall Composite(Clone)");
}
void OnTriggerEnter2D(Collider2D other) {
if (other.name == "Player") {
levelManager.RespawnPlayer();
_Audio.PlayOneShot(deathSound, 1.00f);
Destroy(clone);
}
}
}
Hi. First, and unrelated with your question, if your levelManager is public, you don’t have to look for it on Start(). Or you can make it private and look for it on Start() (whatever you need).
One quick solution that comes to me is that instead destroying the wall, you could reposition it to its original position. If you do this, you will save resources and will avoid getting new wall clones.
So, just add a Vector3 reference to the starting wall position (before Start() add a Vector3 cloneStartingPosition; and inside Start() add cloneStartingPosition=clone.transform.position; and change the Destroy(clone) line with clone.transform.position=cloneStartingPosition (or whatever name you gave to the Vector3 reference).
I’m still a beginner myself, but I think that solution should work…
Edit: of course, if you do that, you should not respawn the wall (I can’t see the script where the wall is respawned, but you should delete the respawn wall line wherever it is).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class KillScript : MonoBehaviour {
public AudioClip deathSound;
AudioSource _Audio;
public LevelManager levelManager;
private GameObject clone;
Vector3 cloneStartingPosition; // this is the Vector3 where we will store the wall starting position
void Start () {
levelManager = FindObjectOfType<LevelManager>(); //as this is a public object, you don't need this line
_Audio = GetComponent<AudioSource>();
clone = GameObject.Find("Wall Composite(Clone)");
cloneStartingPosition=clone.transform.position; // before the wall starts moving, we store the initial position
}
void OnTriggerEnter2D(Collider2D other) {
if (other.name == "Player") {
levelManager.RespawnPlayer();
_Audio.PlayOneShot(deathSound, 1.00f);
clone.transform.position=cloneStartingPosition; //instead of destroying the wall, we move it to the initial position
}
}
}
I tried this but now I’m getting a NullReferenceException.
NullReferenceException: Object reference not set to an instance of an object
KillScript.Start() (at Assets/Scripts/Misc Scripts/KillScripts.cs:22)
Then the wall is instantiated later than this script execution. I’m no expert, but I think you should put the vector3 on the wall script and call it on the killscript modifying this line: clone.transform.position=cloneStartingPosition; calling the cloneStartingPosition from the wall script.
It seems you are quite more beginner than I am (no offense).