I am matching a match-3 game where tiles randomly swap positions with each other and I keep getting the error:
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.
Main.FindMatch (UnityEngine.GameObject[,] cells) (at Assets/Scripts/Main.cs:191)
Main.Update () (at Assets/Scripts/Main.cs:133)
Once this error is thrown no more matches can be made so even if three of the same tiles are next to each other it won’t register as a match. I have no idea what is being destroyed and I’m confused about why the error is referencing three different lines in my script - lines 191, 133, and the FindMatch method “private ArrayList FindMatch(GameObject[,] cells)”
beginning of code with variables and method that shuffles the tiles around
public GameObject _indicator;//The indicator to know the selected tile
public GameObject[,] _arrayOfShapes;//The main array that contain all games tiles
private GameObject _currentIndicator;//The current indicator to replace and destroy each time the player change the selection
private GameObject _FirstObject;//The first object selected
private GameObject _SecondObject;//The second object selected
public GameObject [] _listOfGems;//The list of tiles we cant to see in the game you can remplace them in unity's inspector and choose all what you want
public GameObject _emptyGameobject;//After destroying object they are replaced with this one so we will replace them after with new ones
public GameObject _particleEffect;//The object we want to use in the effect of shining stars
public GameObject _particleEffectWhenMatch;//The gameobject of the effect when the objects are matching
public bool _canTransitDiagonally = false;//Indicate if we can switch diagonally
public int _scoreIncrement;//The amount of point to increment each time we find matching tiles
private int _scoreTotal = 0;//The score
private ArrayList _currentParticleEffets = new ArrayList();//the array that will contain all the matching particle that we will destroy after
public AudioClip MatchSound;//the sound effect when matched tiles are found
public int _gridWidth;//the grid number of cell horizontally
public int _gridHeight;//the grid number of cell vertically
public bool MovingTiles = false;
int x1, y1, x2, y2;
// Use this for initialization
void Start () {
//Initializing the array with _gridWidth and _gridHeight passed in parameter
_arrayOfShapes = new GameObject[_gridWidth, _gridHeight];
//Creating the gems from the list of gems passed in parameter
for ( int i = 0; i <= _gridWidth-1; i++){
for ( int j = 0; j <= _gridHeight-1; j++){
var gameObject = GameObject.Instantiate(_listOfGems[Random.Range(0, _listOfGems.Length)] as GameObject, new Vector3(i, j, 0), transform.rotation) as GameObject;
_arrayOfShapes[i,j]= gameObject;
}
}
//Adding the star effect to the gems and call the DoShapeEffect continuously
InvokeRepeating("DoShapeEffect", 1f, 0.21F);
if (MovingTiles == false)
{
InvokeRepeating ("Shuffle", 1, Random.Range(1,2));
}
else if (MovingTiles == true)
{
CancelInvoke ();
}
}
void Shuffle ()
{
x1 = Random.Range(0, _arrayOfShapes.GetLength(0));
x2 = Random.Range(0, _arrayOfShapes.GetLength(0));
y1 = Random.Range(0, _arrayOfShapes.GetLength(1));
y2 = Random.Range(0, _arrayOfShapes.GetLength(1));
Vector3 temp = _arrayOfShapes[x1, y1].transform.position;
_arrayOfShapes[x1, y1].transform.position = _arrayOfShapes[x2, y2].transform.position;
_arrayOfShapes[x2, y2].transform.position = temp;
GameObject temp2 = _arrayOfShapes[x1, y1];
_arrayOfShapes[x1, y1] = _arrayOfShapes[x2, y2];
_arrayOfShapes[x2, y2] = temp2;
}
here is the area of code that is referenced in the error
{var Matches = FindMatch(_arrayOfShapes); //line 133
//If we find a matched tiles
if (Matches.Count > 0 )
{//Update the score
_scoreTotal += Matches.Count * _scoreIncrement;
foreach (GameObject go in Matches) {
//Playing the matching sound
audio.PlayOneShot(MatchSound);
//Creating and destroying the effect of matching
var destroyingParticle = GameObject.Instantiate(_particleEffectWhenMatch as GameObject, new Vector3(go.transform.position.x, go.transform.position.y, -2), transform.rotation) as GameObject;
Destroy(destroyingParticle, 1f);
//Replace the matching tile with an empty one
_arrayOfShapes[(int)go.transform.position.x, (int)go.transform.position.y] = GameObject.Instantiate(_emptyGameobject, new Vector3((int)go.transform.position.x, (int)go.transform.position.y, -1), transform.rotation) as GameObject;
//Destroy the ancient matching tiles
Destroy(go,0.1f);
}
_FirstObject = null;
_SecondObject = null;
//Moving the tiles down to replace the empty ones
DoEmptyDown(ref _arrayOfShapes);
}
//If no matching tiles are found remake the tiles at their places
else if (_FirstObject != null
&& _SecondObject != null
){
//Animate the tiles
DoSwapMotion(_FirstObject.transform, _SecondObject.transform);
//Swap the tiles in the array
DoSwapTile(_FirstObject, _SecondObject, ref _arrayOfShapes);
_FirstObject = null;
_SecondObject = null;
}
}
//Update the score
(GetComponent(typeof( TextMesh))as TextMesh).text = _scoreTotal.ToString();
}
// Find Match-3 Tile
private ArrayList FindMatch(GameObject[,] cells) //method being referenced in GameObject error??????
{//creating an arraylist to store the matching tiles
ArrayList stack = new ArrayList();
//Checking the vertical tiles
for (var x = 0; x <= cells.GetUpperBound(0); x++)
{
for (var y = 0; y <= cells.GetUpperBound(1); y++)
{
var thiscell = cells[x, y];
//If it's an empty tile continue
if (thiscell.name == "Empty(Clone)") continue;
int matchCount = 0;
int y2 = cells.GetUpperBound(1);
int y1;
//Getting the number of tiles of the same kind
for (y1 = y + 1; y1 <= y2; y1++)
{
if (cells[x, y1].name == "Empty(Clone)" || thiscell.name != cells[x, y1].name) break; //other line referenced in error
matchCount++;
}
//If we found more than 2 tiles close we add them in the array of matching tiles
if (matchCount >= 2)
{
y1 = Mathf.Min(cells.GetUpperBound(1) , y1 - 1);
for (var y3 = y; y3 <= y1; y3++)
{
if (!stack.Contains(cells[x, y3]))
{
stack.Add(cells[x, y3]);
}
}
}
}