Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jiji_ · Oct 10, 2015 at 01:23 PM · c#raycastlist

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]);
         
         }
 
     }
 
 
 }
 
 

alt text

list1.png (23.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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>();

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jiji_ · Oct 10, 2015 at 01:53 PM 0
Share

Thanks a lot for the explanation that solved my problem !

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges