MissingReferenceException: The object of type ‘GameObject’ 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.
Coin.SpawnColumn () (at Assets/Scripts/Coin.cs:48)
Coin.Update () (at Assets/Scripts/Coin.cs:41)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Coin : MonoBehaviour {
public int columnPoolSize = 4;
public GameObject columnPrefab;
public float columnMin = -2.9f;
public float columnMax = 1.4f;
private float spawnXPosition = 16.8f;
private GameObject[] columns;
private Vector2 objectPoolPosition = new Vector2(-14, 0);
private float timeSinceLastSpawned;
public float spawRate;
private int currentColumn;
// Use this for initialization
void Start()
{
columns = new GameObject[columnPoolSize];
for (int i = 0; i < columnPoolSize; i++)
{
columns[i] = Instantiate(columnPrefab, objectPoolPosition, Quaternion.identity);
}
SpawnColumn();
}
// Update is called once per frame
void Update()
{
timeSinceLastSpawned += Time.deltaTime;
if (!GameController.instance.gameOver && timeSinceLastSpawned >= spawRate)
{
timeSinceLastSpawned = 0;
SpawnColumn();
}
}
void SpawnColumn()
{
float spawnYPosition = Random.Range(columnMin, columnMax);
columns[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);
currentColumn++;
if (currentColumn >= columnPoolSize)
{
currentColumn = 0;
}
}
}
[CODE]
Well, it’s all right there in the error message. The game object object referenced has been destroyed. When a game object is destroyed, the C# object still sticks around. Your behaviours can still hold a reference to that object. However, when compared to null it will be true. So even though someObject is not null, it’s holding a reference to an object, someObject == null will still be true. This what’s happening here, you’re holding a reference to a destroyed game object and trying to do something with it. I’m assuming something has destroyed columns[currentColumn] instead of returning it to your object pool.
If you’re using an object pool then you should not be using Destroy. This will remove the object from the hierarchy and I don’t know if there’s any way to bring it back. Instead you should disable it if you intend to use that object again by using SetActive(false). That way it’s “gone” but can be reused by resetting its position, scripts, etc and then using SetActive(true).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinsScore : MonoBehaviour
{
public static CoinsScore Coind;
public GameObject ban;
private void OnTriggerEnter2D(Collider2D collider)
{
if (collider.CompareTag("Player"))
{
GameController.instance.Coin();
GameController.instance.Coinz();
ban.SetActive(false);
}
}
}
Poolcoin
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Coin : MonoBehaviour {
public int columnPoolSize = 4;
public GameObject columnPrefab;
private float columnMin = -2.4f;
private float columnMax = 3.5f;
private float spawnXPosition = 16.8f;
private GameObject[] Coins;
private Vector2 objectPoolPosition = new Vector2(-14, 0);
private float timeSinceLastSpawned;
public float spawRate;
private int currentColumn;
// Use this for initialization
void Start()
{
Coins = new GameObject[columnPoolSize];
for (int i = 0; i < columnPoolSize; i++)
{
Coins[i] = Instantiate(columnPrefab, objectPoolPosition, Quaternion.identity);
}
SpawnColumn();
}
// Update is called once per frame
void Update()
{
timeSinceLastSpawned += Time.deltaTime;
if ( timeSinceLastSpawned >= spawRate)
{
timeSinceLastSpawned = 0;
SpawnColumn();
}
}
void SpawnColumn()
{
float spawnYPosition = Random.Range(columnMin, columnMax);
Coins[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);
currentColumn++;
if (currentColumn >= columnPoolSize)
{
currentColumn = 0;
}
}
}
again gamecontroller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameController : MonoBehaviour {
public static GameController instance;
public GameObject gameOverText;
public bool gameOver;
public float scrollSpeed = -1.5f;
private int score;
public static int coin;
public Text scoreText;
public Text cointext;
private void Awake()
{
if(GameController.instance == null)
{
GameController.instance = this;
}else if(GameController.instance != this)
{
Destroy(gameObject);
Debug.LogWarning("GameController ha sido instanciado por segunda vez. Esto no debería pasar.");
}
}
public void Coinz()
{
PlayerPrefs.SetInt("Coin", coin);
PlayerPrefs.GetInt("Coin");
PlayerPrefs.Save();
}
public void Coin()
{
Coinz();
coin++;
Updateco();
SoundSystem.instance.PlayCoin();
}
public void Updateco()
{
cointext.text = "" + coin;
}
public void BirdScored()
{
if (gameOver) return;
score++;
scoreText.text = "Score: " + score;
}
public void BirdDie()
{
gameOverText.SetActive(true);
gameOver = true;
}
private void OnDestroy()
{
if(GameController.instance == this)
{
GameController.instance = null;
}
}
}