- Home /
After Removing everything from the list it backs to 1?
Hey, so my problem is that after I remove everything from the list and it gets out of the loop it goes back 1.
What I think is there is a problem with using List.add with the raycast It could be not true I am just guessing Can someone tell me the reason please and how to fix it? Here is the code
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Check : MonoBehaviour {
public GameObject[] gob;
public GameObject gob1;
public int checker;
List<GameObject> Cards = new List<GameObject>();
public static int chec = 0;
RaycastHit hit;
public FlipAD s;
void Start () {
Debug.Log ("START!");
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0)) {
if (Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hit)) {
Cards.Add(hit.collider.gameObject); //HERE IS WHERE I ADD TO THE LIST
Debug.Log (hit.collider.tag);
}
}
Debug.Log(Cards.Count);
//Debug.Log (hit.collider.tag);
Debug.Log (chec);
if(Cards.Count == 2){
Debug.Log("Yup Rotation starts here");
float rot = 0f;
while (rot < 180f){
Cards[0].transform.Rotate(new Vector3(0,90,0) * Time.deltaTime);
Cards[1].transform.Rotate(new Vector3(0,90,0) * Time.deltaTime);
rot = rot + 90f * Time.deltaTime;
}
int i =0;
while (Cards.Count !=0){ //HERE WHERE DO I REMOVE EVERYTHING FROM THE LIST
Cards.RemoveAt(i);
i++;
}
} // AFTER LEAVING THE IF CONDITION THE LIST GOES BACK 1 WITHOUT CLICKING ON ANOTHER OBJECT
if (Input.GetMouseButtonDown (0) && (chec) == 2 && hit.collider.tag == "AD") {
Debug.Log ("first if");
gob = GameObject.FindGameObjectsWithTag ("AD");
Invoke ("Destro", 4);
chec = 0;
} else if (Input.GetMouseButtonDown (0) && (chec) == 20 && hit.collider.tag == "D") {
Debug.Log ("first if");
gob = GameObject.FindGameObjectsWithTag ("D");
Invoke ("Destro", 4);
chec = 0;
} else if (Input.GetMouseButtonDown (0) && (chec) == 200 && hit.collider.tag == "S") {
Debug.Log ("first if");
gob = GameObject.FindGameObjectsWithTag ("S");
Invoke ("Destro", 4);
chec = 0;
} else if (Input.GetMouseButtonDown (0) && (chec) == 2000 && hit.collider.tag == "RAK") {
Debug.Log ("first if");
gob = GameObject.FindGameObjectsWithTag ("RAK");
Invoke ("Destro", 4);
chec = 0;
} else if (Input.GetMouseButtonDown (0) && (chec) == 20000 && hit.collider.tag == "FUJ") {
Debug.Log ("first if");
gob = GameObject.FindGameObjectsWithTag ("FUJ");
Invoke ("Destro", 4);
chec = 0;
} else if (Input.GetMouseButtonDown (0) && (chec) == 200000 && hit.collider.tag == "UQ") {
Debug.Log ("first if");
gob = GameObject.FindGameObjectsWithTag ("UQ");
Invoke ("Destro", 4);
chec = 0;
} else if (Input.GetMouseButtonDown (0) && (chec) == 2000000 && hit.collider.tag == "AJ") {
Debug.Log ("first if");
gob = GameObject.FindGameObjectsWithTag ("AJ");
Invoke ("Destro", 4);
chec = 0;
} else if (chec!= 0 && chec != 1 && chec != 2 && chec != 10&& chec != 20&& chec != 100&& chec != 200&& chec != 1000&& chec != 2000&& chec != 10000&& chec != 20000&& chec != 100000&& chec != 200000&& chec != 1000000&& chec != 2000000){
Debug.Log("First False");
chec = 0;
}
}
void Destro(){
for (int i =0; i<2; i++) {
Destroy (gob [i]);
}
}
}
Answer by fafase · Oct 10, 2015 at 01:47 PM
int i =0; while (Cards.Count !=0){ //HERE WHERE DO I REMOVE EVERYTHING FROM THE LIST Cards.RemoveAt(i); i++; }
Think of what you are doing there, i is 0 then remove the first item, then i is 1 and you remove the second item (which was initially the third) . i keeps on increasing, while the list is reducing, I would actually expect a index out of bound exception.
All in all, you wan to empty the list:
list.Clear();
if you want to do it properly:
for(int i = list.Count - 1 ; i >= 0; i--)
{
Item item = list[i];
list.RemoveAt(i);
Destroy(item); // If you need them off
}
list = null;
list = new List<Item>();
Your answer
![](https://koobas.hobune.stream/wayback/20220612040743im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I continue to instantiate an object after deleting 0 Answers
RayCast Nearest Character 0 Answers
A node in a childnode? 1 Answer
compare raycast hit with list of gameobjects c# 2 Answers
Multiple Cars not working 1 Answer