- Home /
Question by
deancampagnolo · Jul 12, 2018 at 07:06 AM ·
respawnfixedupdatedestroy objectmissingreferenceexception
After destroying an object I respawn it and set a variable equal to it. However the next time Fixed Update loops it is equal to null, I have no idea why.
Basically after destroying my player my code re Instantiates it and resets my player variable to the new copy. However for the next Fixed Update player is set to null. After setting the player variable equal to the object in fixed update no problems arise afterwards.
As a side note I am a beginner when it comes to coding and unity so any suggestions would be great, thank you!
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GameMaster : MonoBehaviour {
public static GameMaster gm;// not entirely sure why this is here
public static Transform playerPrefab;
public static Transform beginningSpawnPoint;
public Transform playerPrefabForInspector;
public Transform beginningSpawnPointForInspector;
public float fallOffMap;
private static GameObject player;
public static Vector2 rocketBlastOnPlayer;
public static float minimumMomentumSpeed = 1f;//minimum speed needed to calculate momentum
public static int momentumFactor = 20; // the speed divided by this is then subtracted to the speed (e.g. if speed is 30 with momentumFactor 4 then it would return 30 - (30/4);
private void Awake() {
forInspectorVars();
}
private void forInspectorVars() {//FIXME I am pretty sure that this isn't right
playerPrefab = playerPrefabForInspector;
beginningSpawnPoint = beginningSpawnPointForInspector;
}
private void Start() {
player = GameObject.Find("Player");
}
public static void KillPlayer() {
print(player.name);
Destroy(player);
RespawnPlayer(playerPrefab, beginningSpawnPoint);
print(player.name);
}
public static void DamagePlayer(int amount) {
if(player == null) {
print("OHNO2");
}
player.GetComponent<MainC>().Damage(amount);
}
public static void DamageEnemy(GameObject NPCObject, int amount) {
NPCObject.GetComponent<NPCStats>().Damage(amount);
}
public static void HealEnemy(GameObject NPCObject, int amount) {
NPCObject.GetComponent<NPCStats>().Heal(amount);
}
public static void RespawnPlayer(Transform playerPrefab, Transform beginningSpawnPoint) {
Instantiate(playerPrefab, beginningSpawnPoint.position, beginningSpawnPoint.rotation);
player = GameObject.FindGameObjectWithTag("Player");
if (player == null) {
print("OHNO");
}
}
public static void AddForceToNPC(GameObject theObject, Vector2 force) {
theObject.GetComponent<Rigidbody2D>().AddForce(force, ForceMode2D.Impulse);
}
public static void AddRocketBlastOnPlayer(Vector2 newVelocity) {
rocketBlastOnPlayer += newVelocity;
}
public static Vector2 GetRocketBlastOnPlayer() {
return rocketBlastOnPlayer;
}
public static Vector2 CalculateXMomentum(Vector2 Momentum) {
if (Momentum.x > minimumMomentumSpeed) {
return new Vector2(Momentum.x - (Momentum.x / momentumFactor), Momentum.y);
}
return new Vector2(0, Momentum.y);
}
public IEnumerator IsFallOff() {
while (true) {
}
}
private void FixedUpdate() {
//print(player.name);
rocketBlastOnPlayer = CalculateXMomentum(rocketBlastOnPlayer);
if(player == null) {//FIXME I DONT KNOW WHY THIS BELONGS HERE
player = GameObject.FindGameObjectWithTag("Player");
}
if(player == null) {
print("player is null");
}
/*if(player != null) {
print("yay");
}*/
}
}
Comment
public IEnumerator IsFallOff() {
while (true) {
}
}
This will hang your application. @$$anonymous$$ posted proper solution
Best Answer
Answer by $$anonymous$$ · Jul 12, 2018 at 08:14 AM
Try
player = Instantiate(playerPrefab, beginningSpawnPoint.position, beginningSpawnPoint.rotation);