Unknown Member: Clear???????

Ok, so this is the wierdist error i have ever seen. i created a list and it allows me to use the Clear function, but when i run it with the debuger, it says that Clear is a Unknown Member?

any takers on this?

Here is my code:

public List<Object> blocksOnScreen = new List<Object>();

Instantiate(Blocks [0], SelectRandomLoc(), Quaternion.identity);
blocksOnScreen.Add (Blocks[0]);   <------------------------------------------------------------------ This is where i add this object to my list

blocksOnScreen.Clear();  <------------------------------------------------------------------------------- at my state switch i call clear the screen

when i go threw the debug, it says: Unknown Member: Clear

Does Add and Clear work with Lists inside unity? I know they work with arrays

In C# they do obviously But i’m not sure if it is supported inside unity? Maybe someone else can answer my questions and yours

im sure it is, but then im not sure ive ever tried it.

RemoveAll() is an alternative that I do recall using…

	var foo = new List.<int>();
	foo.Add(1);
	foo.Clear();
	Debug.Log (foo.Count);

Works fine.

Just so you know, clearing a list won’t remove the blocks, all it does is delete all the references in the list. You need to use Destroy if you want to remove GameObjects. Also, you aren’t adding the object you instantiated to the list, you’re just adding the same prefab (or whatever Blocks[0] is) to the list as you’re instantiating. Also you should use List and not List, unless you’re mixing types in the same list.

–Eric

ok, so than how can i clear all the blocks off my screen at the state switch, i have tried everything. noting seams to be working.

Could a for loop maybe work here? I haven’t worked with lists much so I am not currently sure just throwing out suggestions

foreach (GameObject go in blocksOnScreen)
{
    Destroy(go);
}
blocksOnScreen.Clear();

RemoveAll takes a predicate and removes matches. Clear removes everything.

This did not work either. for some reason the items are in the list, the list gets cleared out, but the blocks remain on the screen still?

Thats because you still add the prefab and not the resulting clone into your list. Eric5c5 already mentioned that.

Here is my full script. for some reason no matter how i try to delete the remaining blocks on the screen before the state switch, they stay on there. i have tried foreach, and for loops to go threw the list and delete them, i have tried hard code the instantiate blocks to the specific state and they destroy, them but that failed because it was in update and all it did was create unlimited blocks on the screen, i have tired to do a gameObect.find to locate all the blocks individually with their specific tag and destroy them. I have no idea what else i can try here. for some reason also, when i step through the debugger, when it gets to the Clear function, and hover over it, it says that the Clear part of the function for list is “Unknown Member: Clear”

using UnityEngine;
using System.Collections.Generic;

public class RandomWordGeneratro : MonoBehaviour 
{
	
//======================================================================
	
	#region Declariations
	
	bool isAppleWordComplete 	= false;
	bool isOrangeWordComplete 	= false;
	bool isPearWordComplete 	= false;
	 
	bool isPosArrayLoaded		= false;

	string appleString 			= "Apple";
	string orangeString			= "Orange";
	string pearString			= "Pear";
	
	GameState 					currentState;
	
	Vector3 					Position;
	
	public bool[] 				isDestroyed;
	
	public Texture2D[] 			lettersArray;
	
	public Texture2D[] 			wordsArray;
	
	public GameObject[] 		Blocks;
	
	public Vector3[] 			ScreenSpawnLoc; 
	
	public AudioClip[]			LetterAudio;
	
	public List<GameObject> blocksOnScreen = new List<GameObject>();
	
	public List<KeyValuePair<Vector3, bool>> BlockSpawnLocations = new List<KeyValuePair<Vector3, bool>>();
	
	enum GameState { spellApple, spellOrange, spellPear }
	
	
	#endregion
	
//======================================================================
	
	#region OnGUI

	void OnGUI()
	{
		ChooseWordForGUI();
		
		#region spellApple GUI Textures
		
		if(currentState == GameState.spellApple)
		{
			if(isDestroyed[0] == true)
			{
				GUI.DrawTexture(new Rect(10, 200,  100, 50), lettersArray[0]);
				
			}
			if(isDestroyed[15] == true)
			{
				GUI.DrawTexture(new Rect(110, 200,  100, 50), lettersArray[15]);
				
			}
			if(isDestroyed[15] == true)
			{
				GUI.DrawTexture(new Rect(210, 200,  100, 50), lettersArray[15]);
				
			}
			if(isDestroyed[11] == true)
			{
				GUI.DrawTexture(new Rect(310, 200,  100, 50), lettersArray[11]);
				
			}
			if(isDestroyed[4] == true)
			{
				GUI.DrawTexture(new Rect(410, 200,  100, 50), lettersArray[4]);
				
			}
		}
		
		#endregion
		
		#region spellOrange GUI Textures
		
		if(currentState == GameState.spellOrange)
		{
		    if(isDestroyed[14] == true)
			{
				GUI.DrawTexture(new Rect(10, 200,  100, 50), lettersArray[14]);
				
			}
			if(isDestroyed[17] == true)
			{
				GUI.DrawTexture(new Rect(110, 200,  100, 50), lettersArray[17]);
				
			}
			if(isDestroyed[0] == true)
			{
				GUI.DrawTexture(new Rect(210, 200,  100, 50), lettersArray[0]);
				
			}
			if(isDestroyed[13] == true)
			{
				GUI.DrawTexture(new Rect(310, 200,  100, 50), lettersArray[13]);
				
			}
			if(isDestroyed[6] == true)
			{
				GUI.DrawTexture(new Rect(410, 200,  100, 50), lettersArray[6]);
				
			}
			if(isDestroyed[4] == true)
			{
				GUI.DrawTexture(new Rect(510, 200,  100, 50), lettersArray[4]);
				
			}
		}
		
		#endregion
		
		#region spellPear GUI Textures
		
		if(currentState == GameState.spellPear)
		{
			if(isDestroyed[15] == true)
			{
				GUI.DrawTexture(new Rect(10, 200,  100, 50), lettersArray[15]);
				
			}
			if(isDestroyed[4] == true)
			{
				GUI.DrawTexture(new Rect(110, 200,  100, 50), lettersArray[4]);
				
			}
			if(isDestroyed[0] == true)
			{
				GUI.DrawTexture(new Rect(210, 200,  100, 50), lettersArray[0]);
				
			}
			if(isDestroyed[17] == true)
			{
				GUI.DrawTexture(new Rect(310, 200,  100, 50), lettersArray[17]);
				
			}
		}
		
		#endregion
	}
	
	#endregion
	
//======================================================================
	
	#region Initilize (AKA Start)
	
	// Use this for initialization
	void Start () 
	{
		ChooseState();
	}
	
	#endregion
	
//======================================================================
	
	#region Update
	
	// Update is called once per frame
	void Update () 
	{
//======================================================================
			
		#region GameStateApple
		
		switch (currentState)
		{
			case GameState.spellApple:

				if(Input.GetMouseButton(0))
			    {
					Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
					RaycastHit hit;
		
					if(Physics.Raycast(ray, out hit))
					{
						// Conditionals for Hitting the proper blocks
						
					if( appleString.Contains("A")  hit.transform.gameObject.tag == "Ablock" )
					{
							Destroy(hit.transform.gameObject);
							
							audio.PlayOneShot(LetterAudio[0]);

							isDestroyed[0] = true; 
					}
					
					if( appleString.Contains("p")  hit.transform.gameObject.tag == "Pblock" )
					{
							Destroy(hit.transform.gameObject);
						
							audio.PlayOneShot(LetterAudio[15]);

							isDestroyed[15]= true; 
					}
					
					if( appleString.Contains("p")  hit.transform.gameObject.tag == "Pblock" )
					{
							Destroy(hit.transform.gameObject);
						
							audio.PlayOneShot(LetterAudio[15]);
						
							isDestroyed[15] = true;
					}
					
					if( appleString.Contains("l")  hit.transform.gameObject.tag == "Lblock" )
					{
							Destroy(hit.transform.gameObject);
						
							audio.PlayOneShot(LetterAudio[11]);
						
							isDestroyed[11] = true; 
						
					}
					
					if( appleString.Contains("e")  hit.transform.gameObject.tag == "Eblock" )
					{
							Destroy(hit.transform.gameObject);
						
							audio.PlayOneShot(LetterAudio[4]);
						
							isDestroyed[4] = true; 
					}
				}
			}
			if(isDestroyed[0] == true  isDestroyed[15] == true  isDestroyed[11] == true  isDestroyed[4] == true)
			{
				isAppleWordComplete = true;
				
				if(isAppleWordComplete == true)
				{
					isDestroyed	[0] 	= false;
					isDestroyed	[15] 	= false;
					isDestroyed	[11] 	= false;
					isDestroyed	[4] 	= false;
					
					foreach (GameObject go in blocksOnScreen)
					{
						Destroy(go);
					}
					
					blocksOnScreen.Clear();

					currentState = GameState.spellOrange;	// Change the State

					CreateOrangeBlocks();

				}
			}

		break;
			
	#endregion
			
//======================================================================
			
	    #region GameStateOrange
			
		case GameState.spellOrange:
			
			Debug.Log (" In spellOrange State "); 

			if(Input.GetMouseButton(0))
			    {
					Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
					RaycastHit hit;
		
					if(Physics.Raycast(ray, out hit))
					{
						// Conditionals for Hitting the proper blocks
						
					if( orangeString.Contains("O")  hit.transform.gameObject.tag == "Oblock" )
					{
							Destroy(hit.transform.gameObject);
						
							audio.PlayOneShot(LetterAudio[14]);

							isDestroyed[14] = true; 
					}
					
					if( orangeString.Contains("r")  hit.transform.gameObject.tag == "Rblock" )
					{
							Destroy(hit.transform.gameObject);
						
							audio.PlayOneShot(LetterAudio[17]);

							isDestroyed[17] = true; 
					}
					
					if( orangeString.Contains("a")  hit.transform.gameObject.tag == "Ablock" )
					{
							Destroy(hit.transform.gameObject);
						
							audio.PlayOneShot (LetterAudio[0]);
						
							isDestroyed[0] = true;
					}
					
					if( orangeString.Contains("n")  hit.transform.gameObject.tag == "Nblock" )
					{
							Destroy(hit.transform.gameObject);
						
							audio.PlayOneShot (LetterAudio[13]);
						
							isDestroyed[13] = true; 
						
					}
					if( orangeString.Contains("g")  hit.transform.gameObject.tag == "Gblock" )
					{
							Destroy(hit.transform.gameObject);
						
							audio.PlayOneShot (LetterAudio[6]);
						
							isDestroyed[6] = true; 
					}
					
					if( orangeString.Contains("e")  hit.transform.gameObject.tag == "Eblock" )
					{
							Destroy(hit.transform.gameObject);
						
							audio.PlayOneShot (LetterAudio[4]);
						
							isDestroyed[4] = true; 
					}
				}
			}
			if(isDestroyed[14] == true  isDestroyed[17] == true  isDestroyed[0] == true  isDestroyed[13] == true
				 isDestroyed[6] == true  isDestroyed[4] == true)
			{
				isOrangeWordComplete = true;
				
				if(isOrangeWordComplete == true)
				{
					isDestroyed	[14] 	= false;
					isDestroyed [17] 	= false;
					isDestroyed  [0] 	= false;
					isDestroyed	[13] 	= false;
					isDestroyed  [6] 	= false;
					isDestroyed	 [4] 	= false;
					
					foreach (GameObject go in blocksOnScreen)
					{
						Destroy(go);
					}
					
					blocksOnScreen.Clear();

					currentState = GameState.spellPear;

					CreatePearBlocks();

				
				}
			}

			break;
			
	#endregion
			
//======================================================================
			
	    #region GameStatePear
			
		case GameState.spellPear:
			
			Debug.Log (" In spellPear State ");
			
				if(Input.GetMouseButton(0))
			    {
					Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
					RaycastHit hit;
		
					if(Physics.Raycast(ray, out hit))
					{
						// Conditionals for Hitting the proper blocks
						
					if( pearString.Contains("P")  hit.transform.gameObject.tag == "Pblock" )
					{
							Destroy(hit.transform.gameObject);
						
							audio.PlayOneShot (LetterAudio[15]);

							isDestroyed[15] = true; 
					}
					
					if( pearString.Contains("e")  hit.transform.gameObject.tag == "Eblock" )
					{
							Destroy(hit.transform.gameObject);
						
							audio.PlayOneShot (LetterAudio[4]);

							isDestroyed[4] = true; 
					}
					
					if( pearString.Contains("a")  hit.transform.gameObject.tag == "Ablock" )
					{
							Destroy(hit.transform.gameObject);
						
							audio.PlayOneShot (LetterAudio[0]);
						
							isDestroyed[0] = true;
					}
					
					if( pearString.Contains("r")  hit.transform.gameObject.tag == "Rblock" )
					{
							Destroy(hit.transform.gameObject);
						
							audio.PlayOneShot (LetterAudio[17]);
						
							isDestroyed[17] = true;
					}
				}
			}
			if(isDestroyed[15] == true  isDestroyed[4] == true  isDestroyed[0] == true  isDestroyed[17] == true)
			{
				isPearWordComplete = true;
				
				if(isPearWordComplete == true)
				{
					isDestroyed	[15] 	= false;
					isDestroyed  [4] 	= false;
					isDestroyed  [0] 	= false;
					isDestroyed	[17] 	= false;
					
					foreach (GameObject go in blocksOnScreen)
					{
						Destroy(go);
					}
					
					blocksOnScreen.Clear();

					currentState = GameState.spellApple;	// Change the State

					CreateAppleBlocks();
				}
			}

			break;
		}
	}
	
	#endregion
	
//======================================================================
	
	#endregion
	
//======================================================================
	
	#region ChooseState
	
	void ChooseState()
	{
		if(!isPosArrayLoaded)
		{
			ResetPositionArray();
		}
		int rnd = Random.Range(0, 3);	// Generate a random number to choose what state to be in 
		
		if(rnd == 0)
		{
			currentState = GameState.spellApple;
			CreateAppleBlocks();
			Debug.Log (" In spellApple State ");
		}
		else if(rnd == 1)
		{
			currentState = GameState.spellOrange;
			CreateOrangeBlocks();
			Debug.Log (" In spellOrange State ");
		}
		else if(rnd == 2)
		{
			currentState = GameState.spellPear;
			CreatePearBlocks();
			Debug.Log (" In spellPear State ");
		} 
	}
	
	#endregion
	
//======================================================================
	
	#region ChooseWordForGUI
	
	void ChooseWordForGUI()
	{
		if( currentState == GameState.spellApple)
		{
			//Rect (left, top, width, height)
			
			GUI.DrawTexture(new Rect(10, 10,  200, 100), wordsArray[0]);
		}
		else if( currentState == GameState.spellOrange)
		{
			//Rect (left, top, width, height)
				
			GUI.DrawTexture(new Rect(10, 10,  200, 100), wordsArray[1]);
		}
		else
		{
			//Rect (left, top, width, height)
				
			GUI.DrawTexture(new Rect(10, 10,  200, 100), wordsArray[2]);
		}
	}
	
	
	
	#endregion
	
//======================================================================
	
	#region SelectRandomLoc
	
	Vector3 SelectRandomLoc()
	{
		int rnd = Random.Range(0, 10);
		
		while(BlockSpawnLocations[rnd].Value == true)
		{
			rnd = Random.Range (0, 10);
		}
		
		//BlockSpawnLocations[rnd].Value = true; // Value is a read-only!!! 
		
		Vector3 hold = BlockSpawnLocations[rnd].Key; // Grabs the vector 3
		
		BlockSpawnLocations[rnd] = new KeyValuePair<Vector3, bool>(hold, true);		// Replaces the KVP with a KVP with a true value
		
		return BlockSpawnLocations[rnd].Key;
		
	}
	
	#endregion
	
//======================================================================
	
	#region Create AppleBlocks
	
	public void CreateAppleBlocks()
	{
		ResetPositionArray();
		
		Instantiate(Blocks [0], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[0]);
		
		Instantiate(Blocks[15], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[15]);
		
		Instantiate(Blocks[15], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[15]);
		
		Instantiate(Blocks[11], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[11]);
		
		Instantiate(Blocks [4], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[4]);
		
		int rnd2 = Random.Range(0, 25);
		Instantiate(Blocks [rnd2], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[rnd2]);
		
		int rnd3 = Random.Range(0, 25);
		Instantiate(Blocks [rnd3], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[rnd3]);
		
		int rnd4 = Random.Range(0, 25);
		Instantiate(Blocks [rnd4], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[rnd4]);
		
		int rnd5 = Random.Range(0, 25);
		Instantiate(Blocks [rnd5], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[rnd5]);
		
		int rnd = Random.Range(0, 25);
		Instantiate(Blocks [rnd], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[rnd]);
			
	}
	
	#endregion
	
//======================================================================
	
	#region CreateOrangeBlocks
	
	void CreateOrangeBlocks()
	{
		ResetPositionArray();

		Instantiate(Blocks  [0], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[0]);
		
		Instantiate(Blocks  [4], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[4]);
		
		Instantiate(Blocks  [6], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[6]);
		
		Instantiate(Blocks [13], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[13]);
		
		Instantiate(Blocks [14], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[14]);
		
		Instantiate(Blocks [17], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[17]);

		int rnd2 = Random.Range(0, 25);
		Instantiate(Blocks [rnd2], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[rnd2]);
		
		int rnd3 = Random.Range(0, 25);
		Instantiate(Blocks [rnd3], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[rnd3]);
		
		int rnd4 = Random.Range(0, 25);
		Instantiate(Blocks [rnd4], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[rnd4]);
		
		int rnd5 = Random.Range(0, 25);
		Instantiate(Blocks [rnd5], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[rnd5]);
		
	}
	
	
	#endregion
	
//======================================================================
	
	#region CreatePearBlocks
	
	void CreatePearBlocks()
	{
		ResetPositionArray();

		Instantiate(Blocks [15], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[15]);
		
		Instantiate(Blocks  [4], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[4]);
		
		Instantiate(Blocks  [0], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[0]);
		
		Instantiate(Blocks [17], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[17]);
		
		int rnd2 = Random.Range(0, 25);
		Instantiate(Blocks [rnd2], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[rnd2]);
		
		int rnd3 = Random.Range(0, 25);
		Instantiate(Blocks [rnd3], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[rnd3]);
		
		int rnd4 = Random.Range(0, 25);
		Instantiate(Blocks [rnd4], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[rnd4]);
		
		int rnd5 = Random.Range(0, 25);
		Instantiate(Blocks [rnd5], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[rnd5]);
		
		int rnd6 = Random.Range(0, 25);
		Instantiate(Blocks [rnd6], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[rnd6]);
		
		int rnd7 = Random.Range(0, 25);
		Instantiate(Blocks [rnd7], SelectRandomLoc(), Quaternion.identity);
		blocksOnScreen.Add (Blocks[rnd7]);

	}
	
	#endregion
	
//======================================================================
	
	#region ResetPositionArray
	
	void ResetPositionArray()
	{
		BlockSpawnLocations = new List<KeyValuePair<Vector3, bool>>();
		
		for(int i = 0; i <= 9; ++i)
		{
			BlockSpawnLocations.Add (new KeyValuePair<Vector3, bool>(ScreenSpawnLoc[i], false));
		}
	}
	
	#endregion
	
//======================================================================
	
	// Convert array to list
	
	//private List<object> ConvertArrayToList(object[] array)
	//{
		//List<Object> list = new List<Object>();
		
		//foreach (Object obj in array)
		//list.Add (obj);
		
		//return list; 
	//}
		
//======================================================================

	#region EndBracde
	
}

	#endregion 

//======================================================================

im not sure on how else to try to do that. i dont understand how this would not add the instantiated object to the list?
i understand what you are saying that im just puting another clone of rnd7 in the list and not the Instantiate one, but i have no idea on how to add the Instantiated on into a list?

int rnd7 = Random.Range(0, 25);
Instantiate(Blocks [rnd7], SelectRandomLoc(), Quaternion.identity);
blocksOnScreen.Add (Blocks[rnd7]);

so is this what you mean

blocksOnScreen.Add ((GameObject)Instantiate(Blocks [rnd7], SelectRandomLoc(), Quaternion.identity));

Yes.

holey crap, that worked like a charm. had idea that it would even work!!!

blocksOnScreen.Add ((GameObject)Instantiate(Blocks [rnd], SelectRandomLoc(), Quaternion.identity));

Thanks to everyone for the help…