Completely stumped. (Destroying instated objects)/Bug

OK I’ve been trying to fix this for two days now. I honestly have no idea what is going on.

The script is designed to randomly select three prefabs from an array that stores them.

Display them.

Add the clicked object to a list stored in the player prefab

wipe out objects on screen

then select 3 now ones and continue on.

Everything works in the script EXCEPT that when the script runs makeCard1() after the first time some of the objects aren’t destroyed, I’m not sure how to explain it so I’ve posted an image of the problem and the code for the class as well.

What am I doing wrong here?

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class draftDeckBuilder : MonoBehaviour {

	// Use this for initialization
	
	public GameObject player;
	
	private int temp1Number = 0;
	private int temp2Number = 0;
	private int maxRange;
	
	void Start () {
		
		player.GetComponent<player>().inArena = true;
		
		player.GetComponent<player>().draftDeck.GetComponent<draftDeck>().reset();
		
		maxRange = player.GetComponent<player>().cardLibrary.GetComponent<cardLibrart1>().count; //obtains current amount of cards to pick from
	}
	
	public bool nextPick = true; //tells the update when to call makeNExt again
	
	public GameObject temp1; //these objects will be instated
	public GameObject temp2;
	public GameObject temp3;
	
	public int count = 0;
	
	// Update is called once per frame
	void Update ()
	{
		if(count < 30) //limit
		{
			if(nextPick) //if the user has selected an object the objects will be cleared fro mthe scene and cleaned
			{
					DestroyImmediate(temp1);
					DestroyImmediate(temp2); //destorys the objects
					DestroyImmediate(temp3);
					makeCard1();	//starts instanting new objects
			}
			else
			{
				if(Input.GetMouseButtonDown(0)) //if left clicked
				{
					Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
       				RaycastHit hit;
				
					if( Physics.Raycast( ray, out hit, 100 ) )
       				{
						player.GetComponent<player>().draftDeck.GetComponent<draftDeck>().addCard(hit.transform.gameObject);//adds the selected card to list
						nextPick = true;//time to make a new selection
					}
				}
			}
		}
	
	}

	void makeCard1()//sets the card to the left
	{
		temp1Number = 0; temp2Number =0;
		
		int m = Random.Range(0, maxRange);
		
		temp1 = Instantiate(player.GetComponent<player>().cardLibrary.GetComponent<cardLibrart1>().cardLibrary[m],new Vector3((float)2.2,0,0),Quaternion.identity) as GameObject;
		temp1Number = m;
		
		makeCard2();//calls next method
	}
	
	void makeCard2() //center card
	{
			int m = Random.Range(0, maxRange);
		
			while(m == temp1Number)
			{
				m = Random.Range(0, maxRange);
			}
		
			temp2 = Instantiate(player.GetComponent<player>().cardLibrary.GetComponent<cardLibrart1>().cardLibrary[m],new Vector3(0,0,0),Quaternion.identity) as GameObject;
			temp2Number = m;
		
			makeCard3();//calls final method
	}
	
	void makeCard3()//right card
	{
		int m = Random.Range(0, maxRange);
		
		while(m == temp1Number || m == temp2Number)
		{
			m = Random.Range(0, maxRange);
		}
		
		temp3 = Instantiate(player.GetComponent<player>().cardLibrary.GetComponent<cardLibrart1>().cardLibrary[m],new Vector3((float)-2.2,0,0),Quaternion.identity) as GameObject;
		nextPick = false;//prevents making new cards
	}
}

Not reviewed all your code, but you should replace DestroyImmediate() to Destroy()

It was destroy then I changed it hoping it would fix it by getting rid of it before the frame ended. (I’ve tried like everything).

I’ll suggest the following:

  • Put all cards on Gameobject[ ] array instead of card1, card2, card3, etc.
  • Make one unique function to generate cards, you can pass a number to identify which card to generate
  • Raycast only against cards gameobjects, use a Layermask to filter out other objects