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