- Home /
Other
How do I make the camera stop moving after a gameobject is destroyed?
I want the camera to freeze after the player/gameobject is destroyed. However, when the player is destroyed, the game just freezes and it gives me this error "System.NullReference Exception has been thrown. Object reference not set to an instance of an object." I don't know what to change. This is what I have right now:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class DestroyContact : MonoBehaviour {
PlayerController playerMovement;
CameraController cameraMovement;
public GameObject player;
public Camera camera;
public ParticleSystem particleSystemPlayer;
public Vector3 PosofDeadPlayer;
void Awake()
{
playerMovement = GetComponent <PlayerController> ();
cameraMovement = GetComponent<CameraController>();
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "Floor") {
return;
}
if (other.gameObject.tag != "Floor") {
Destroy (gameObject);
PlayerisDead ();
}
}
void PlayerisDead()
{
playerMovement.enabled = false;
cameraMovement.enabled = false; //this is where the error message is
PosofDeadPlayer = player.transform.position;
particleSystemPlayer.transform.position = PosofDeadPlayer;
particleSystemPlayer.Play (true);
}
}
Answer by Rugbug_Redfern · Jan 13, 2018 at 06:02 PM
It's probably because you have Destroy(gameObject) before you actually run the code to stop the camera. Try putting PlayerisDead(); before Destroy(gameObject);.
I put PlayerisDead(); before Destroy(gameObject); but it still gives me the same error.