- Home /
,MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.
I have problem that my player is not spawning MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
{ public static GameMaster gm;
[SerializeField]
private int maxLives = 3;
private static int _remainingLives;
public static int RemainingLives
{
get { return _remainingLives; }
}
[SerializeField]
private int startingMoney;
public static int Money;
void Awake()
{
if (gm == null)
{
gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>();
}
}
public Transform playerPrefab;
public Transform spawnPoint;
public float spawnDelay = 2;
public Transform spawnPrefab;
public string respawnCountdownSoundName = "RespawnCountdown";
public string spawnSoundName = "Spawn";
public string gameOverSoundName = "GameOver";
public CameraShake cameraShake;
[SerializeField]
private GameObject gameOverUI;
[SerializeField]
private GameObject upgradeMenu;
public delegate void UpgradeMenuCallback(bool active);
public UpgradeMenuCallback onToggleUpgradeMenu;
//cache
private AudioManager audioManager;
private UnityEngine.Object clone;
private object _enemy;
void Start()
{
if (cameraShake == null)
{
Debug.LogError("No camera shake referenced in GameMaster");
}
_remainingLives = maxLives;
Money = startingMoney;
//caching
audioManager = AudioManager.instance;
if (audioManager == null)
{
Debug.LogError("FREAK OUT! No AudioManager found in the scene.");
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.U))
{
ToggleUpgradeMenu();
}
}
private void ToggleUpgradeMenu()
{
upgradeMenu.SetActive(!upgradeMenu.activeSelf);
onToggleUpgradeMenu.Invoke(upgradeMenu.activeSelf);
}
public void EndGame()
{
audioManager.PlaySound(gameOverSoundName);
Debug.Log("GAME OVER");
gameOverUI.SetActive(true);
}
public IEnumerator _RespawnPlayer()
{
yield return new WaitForSeconds(spawnDelay);
Instantiate(playerPrefab, spawnPoint.position, spawnPoint.rotation);
Transform clone = (Transform)Instantiate(spawnPrefab, spawnPoint.position, spawnPoint.rotation);
}
public static void KillPlayer(Player player)
{
Destroy(player.gameObject);
_remainingLives -= 1;
if (_remainingLives <= 0)
{
gm.EndGame();
}
else
{
gm.StartCoroutine(gm._RespawnPlayer());
}
}
public static void KillEnemy(Enemy enemy)
{
gm._KillEnemy(enemy);
}
public void _KillEnemy(Enemy _enemy)
{
// Let's play some sound
audioManager.PlaySound(_enemy.deathSoundName);
// Add particles
GameObject _clone = Instantiate(_enemy.deathParticles, _enemy.transform.position, Quaternion.identity) as GameObject;
Destroy(_clone, 5f);
}
private class AudioManager
{
internal static readonly AudioManager instance;
internal void PlaySound(string gameOverSoundName)
{
throw new NotImplementedException();
}
}
} ,
I thought about reading through your code to figure out what line the error is on, but thought better of it. Post the full error or at least tell us which line is causing it :-/
even unity is not telling me where is the error in my C# coding jst this ($$anonymous$$issingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, UnityEngine.Vector3 pos, UnityEngine.Quaternion rot) (at :0) UnityEngine.Object.Instantiate (UnityEngine.Object original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at :0) UnityEngine.Object.Instantiate[T] (T original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at :0) Game$$anonymous$$aster+<RespawnPlayer>d_27.$$anonymous$$oveNext () (at Assets/Player/Game$$anonymous$$aster.cs:100) UnityEngine.SetupCoroutine.Invoke$$anonymous$$oveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at :0) )
That there tells you part of the error is traced through line 100 of Game$$anonymous$$aster.cs, so it would be helpful to include the entire Game$$anonymous$$aster.cs script in your question and to also point out which line 100 is
Your answer
Follow this Question
Related Questions
How to diappear a line renderer from start and end position of line 1 Answer
MissingReferenceException Help 1 Answer
Child instantiated object to RayCastHit's parent. 1 Answer
MissingReferenceException error. 1 Answer
Camera Orbiting Character: How to make an object move with another with no rotation 1 Answer