Learning the List

I need a little help solving the last bit of this. How would I get the name (string) of the random continent that was removed (RemoveAt) when the PickOne function is called?

If needed, this List consists of 6 continents:
africa, asia, europe etc.. which are all strings and when the button is clicked it gives me a random integer but how would I get the string or name of the continent? Using Random.Range was the only way I could figure out to get one of the Continents randomly.

Here’s what I have so far and I’ve learned a lot! :slight_smile: How to Add, RemoveAt, Clear and the List itself so Awesome but now Im struggling at the end which may not be a list question at all, but I need help.

I commented out the unimportant stuff in case someone else wants to use it to help learn.

#pragma strict
import System.Collections.Generic;

var continents : List.<String> = new List.<String>();
var populateFrom : List.<String>;

/*
function Start () 
{
	continents.Add("antarctica"); //adds yoo to the list
	
	//print(continents.Capacity);
	//continents.Clear();//removes all
	
	
	for(var something in continents)
     {
         print(something);
     }
	
	continents.Remove("asia");//it works it removes asia from the list
	
	for(var something in continents)
     {
         print(something);
     }
}
*/
function OnGUI()
{
	/*
	if (GUI.Button (Rect (20,40,80,20), "Add"))
	{
		if(continents.Contains("antarctica"))
		{
			print("you cant add that");//works if I click
		}
		else
		{
			continents.Add("antarctica");
			print("Added when continents.Add(antarctica); is commented out");//works as well
		}
	}
	*/
	if (GUI.Button (Rect (20,80,80,20), "Get Random"))
	{
		PickOne();	
	}
}
//my failed attempt//almost there
function PickOne()
{
	if(continents.Count == 0)
	{
		ResetDeck();//maybe put in start later?
	}
	
	var	select : int = Random.Range(0, continents.Count);
	continents.RemoveAt(select);
	print(select);
}
function ResetDeck()
{
    continents.Clear();
    continents.AddRange(populateFrom);
}

In order to retrieve an object inside the list that has been removed, you’ll need to store it in a temporary object and return it after removing it from the list.

// C#, sorry, I'm really bad at converting to JS
string PickContinent()
{
    int select = Random.Range(0, continents.Count); // picks a number between the min (inclusive) and max (exclusive)
    string temp = continents[select];
    continents.RemoveAt(select);
    return temp;
}

Thank You wolfhunter777 I really appreciate you taking the time it works :slight_smile:
Its now in the memory bank :slight_smile:

var	select : int = Random.Range(0, continents.Count);
var temp : String = continents[select];
continents.RemoveAt(select);
//return temp; //looking up the meaning of return right after this
//print(select);
print(temp);

again Thank You!

This is incorrect; it should be “Random.Range(0, continents.Count);”.

–Eric

Not sure why I’ve always put the -1 a habit I picked up along the way, maybe it was .length related? Is there a time when you would use .Count-1 or never? Im guessing never.

I wish I had your knowledge Eric :slight_smile: Thanks!

When you use list.Count - 1, you are getting the last index of the list. Meaning, if you have a list with 10 items, then the last index is 9 (item count 10 minus 1). You do this because the first index is always 0 (0, 1, 2, 3, 4, 5, 6, 7, 8, 9 is 10 items).

You don’t use this in Random.Range because the number returned will be 0 or a number less than list.Count. By tacking -1 to the count, you say the returned number will be 0 or less than the last index, so you would never get to pick the last item in the list (which would be problematic if there is only one item in the list. Otherwise, you might not notice that it is a problem).

I hope that isn’t too confusing.

You wouldn’t use it in Random.Range, but you’d use it to get the last item in the list.

–Eric

Thanks guys that makes perfect sense :smile: