check if a list contains the same id as in another list
Hi all
First of all i don't normal ask for help because the whole part of learning to code is to do it your self but this one has been bugging me for nearly a week.
For days now I've been trying to loop though all the available pets in the (petsInDatabase list), to check if the user has seen or captured any of the pets, by its petID. By searching through and comparing against the users (usersPetIndexInDatabase list) of all the users seen and captured pets.
If a pet id is matched in both lists, then a check is made to see if the pet with that id, was seen or caught and will instantiate the correct image game object, else if there isn't a match then instantiate a empty game object.
i finally found a way, using the idea from this link: [link text][1] [1]: http://answers.unity3d.com/questions/794811/check-if-list-contains-item-with-matching-string-p.html but every time i run unity and click the UI button, that calls the LoadPetIndexFromDatabase() method, it fezzes unity and I have to false quit the unity editor.
Below is the method that gets executed from the UI Button, this method works fine as this is from another c# script from my project.
The petsInDatabaseScriptList is just a list that will hold all the scripts attached to the instantiated pet game objects, within the (Pet Index List Manager script)
public void LoadPetIndexFromDatabase()
{
if (petsInDatabaseScriptList.Count < petDatabaseSerializer.petDatabase.petsInDatabase.Count)
{
for (int i = 0; i < petDatabaseSerializer.petDatabase.petsInDatabase.Count; i++)
{
SearchPetsAvailable(petDatabaseSerializer.petDatabase.petsInDatabase[i].PetID);
Debug.Log("pets in database increment id " + i);
}
Debug.Log("Successfully Added all the pets into the users pet index list");
}else{
Debug.Log("Already added all the pets into the users pet index list!!");
}
}
Below is the SearchPetsAvailable method that get executed from within the body of the above method LoadPetIndexFromDatabase(). The problem lies within the for loop in the below method, because when i comment the whole for loop out, the unity console logs the (pet id converted)
public void SearchPetsAvaiable(string _petID)
{
int petID = Convert.ToInt32(_petID);
//bool isInList = false;
Debug.Log("pet id converted = " + petID);
for(int i = 0; i < usersDatabaseSerializer.userDatabase.usersPetIndexInDatabase.Count;)
{
Debug.Log("pets in pet index increasment id " + i);
//if the pet ID From the avaiable pets, can be found in the users pet index, then check the status
if (usersDatabaseSerializer.userDatabase.usersPetIndexInDatabase[i].PetID == petID)
{
if (usersDatabaseSerializer.userDatabase.usersPetIndexInDatabase[i].PetStatus == "seen")
{
Debug.Log("seen " + i);
AddSeenPetToPetIndexList(usersDatabaseSerializer.userDatabase.usersPetIndexInDatabase[i].PetIndexID, usersDatabaseSerializer.userDatabase.usersPetIndexInDatabase[i].PetID);
}else if (usersDatabaseSerializer.userDatabase.usersPetIndexInDatabase[i].PetStatus == "caught"){
Debug.Log("caught " + i);
AddCaughtPetToPetIndexList(usersDatabaseSerializer.userDatabase.usersPetIndexInDatabase[i].PetIndexID, usersDatabaseSerializer.userDatabase.usersPetIndexInDatabase[i].PetID);
}
}else{
Debug.Log("un " + i);
AddUnknownPetToPetIndexList(petID);
}
}
}
it would be great if someone could help me and any others that are trying to accomplish this, or point me in the right direction
thanks for any help
Answer by thePeine · Jun 25, 2017 at 09:50 PM
It looks like you're missing the i++ in the bottom loop. So your'e just looking at [0] over and over again infinitely.
Thank you very much, your correct it was the i++, but i did fix it that night but then i forgot to come on here and say Ive fixed it. I missed it out because i normal copy and past for quickness. But for the above incorrect for loop i wrote it out quick, and like you pointed out it was the i++.
so thank you any way
cheers ricky
Your answer
Follow this Question
Related Questions
Removing items from list by OnTriggerExit 2 Answers
C# list type not working 2 Answers
LoadScene not working 1 Answer
Tracking Long Lists and Scalability 0 Answers
List instance is always the same as the blueprint ? 0 Answers