Hi everyone, im doing a small game for a project but i’m a bit of a noob when it comes to game developing.
The scene is a card game, like a memory, everything works fine untill i win, go back to main menu and choice to play again, then as soon as i click the second card, i get this error:
MissingReferenceException: The object of type ‘Card’ 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.Component.get_gameObject () (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineComponent.cs:172)
Card.CardSelected (UnityEngine.GameObject g) (at Assets/Script/Card.cs:58)
CardsHolder.OnClick (UnityEngine.GameObject g) (at Assets/Script/CardsHolder.cs:58)
Card+c__Iterator1.MoveNext () (at Assets/Script/Card.cs:54)
Those are the two scripts used in this scene:
using UnityEngine;
using System.Collections;
public class CardsHolder : MonoBehaviour {
public GameObject[] cards;
int cardsClicked, numberOfCorrectedAnswers;
int cardNumber1, cardNumber2;
GameObject card1, card2;
public delegate void OnCardsSelection(GameObject g);
public static event OnCardsSelection CardsSelected;
void Awake(){
}
// Use this for initialization
void Start () {
Card.OnClick+=OnClick;
cardNumber1=-1;
cardNumber2=-1;
numberOfCorrectedAnswers=0;
cardsClicked=0;
Shuffle(cards);
//card1 = new GameObject();
//card2 = new GameObject();
}
// Update is called once per frame
void Update () {
if(numberOfCorrectedAnswers==3){
Application.LoadLevel("Scene0");
}
}
void OnClick(GameObject g){
if(cardsClicked<=2){
cardsClicked++;
if(cardsClicked==1){
cardNumber1=g.GetComponent<Card>().cardnum;
card1=g;
}
if(cardsClicked==2){
cardNumber2=g.GetComponent<Card>().cardnum;
card2=g;
}
//Debug.Log(cardNumber1);
//Debug.Log(cardNumber2);
if(cardsClicked>=2){
if(cardNumber1==cardNumber2){
card1.renderer.enabled=false;
card2.renderer.enabled=false;
numberOfCorrectedAnswers++;
}else{
Debug.Log("carta uno " + card1);
if(card1) CardsSelected(card1);
if(card2) CardsSelected(card2);
}
cardsClicked=0;
}
}
}
private void Shuffle(GameObject[] cards){
for(int i=0; i<cards.Length; i++){
int rng = Random.Range(i, cards.Length);
Vector3 tempCard = cards*.transform.position;*
_ cards*.transform.position=cards[rng].transform.position;_
_ cards[rng].transform.position=tempCard; _
_ }*_
* }*
}
and
using UnityEngine;
using System.Collections;
public class Card : MonoBehaviour {
* public Sprite frontCard;*
* public int cardnum;*
* Sprite backCard;*
* Vector3 backCardScale;*
* bool flipped, clickActive;*
* SpriteRenderer spriteRenderer;*
* public delegate void OnClickEvent(GameObject g);*
* public static event OnClickEvent OnClick;*
* // Use this for initialization*
* void Start () {*
* flipped = false;*
* clickActive = true;*
* spriteRenderer = gameObject.GetComponent();*
* backCard=spriteRenderer.sprite;*
* backCardScale = gameObject.transform.localScale;*
* CardsHolder.CardsSelected += CardSelected;*
* }*
* // Update is called once per frame*
* void Update () {*
* }*
* void OnMouseDown(){*
* if(clickActive){*
* Debug.Log(gameObject.tag);*
* if(flipped){*
* gameObject.transform.localScale = backCardScale;*
* spriteRenderer.sprite=backCard;*
* flipped=false;*
* }else{*
* gameObject.transform.localScale = new Vector3(1.3f, 1.3f, 0);*
* spriteRenderer.sprite=frontCard;*
* flipped=true;*
* gameObject.collider2D.enabled=false;*
* StartCoroutine(Wait());*
* }*
* }*
* }*
* IEnumerator Wait(){*
* clickActive=false;*
* yield return new WaitForSeconds (1.0f);*
* OnClick(gameObject);*
* }*
* void CardSelected(GameObject g){*
* if(g.tag==gameObject.tag){*
* //Debug.Log(gameObject.tag + " riattivato il collider");*
* gameObject.collider2D.enabled=true;*
* gameObject.transform.localScale = backCardScale;*
* spriteRenderer.sprite=backCard;*
* flipped=false;*
* }else{*
* //Debug.Log(“Questa carta è:” + gameObject.tag + "mentre la carta passata è: " + g.tag);*
* }*
* clickActive=true;*
* }*
}