- Home /
(c#) display a random string at game over
Hey all, I'm trying to set up a game over script that will display a random string of text.
public void GameOver()
{
string[] dead = new List
{"gameover1",
"gameover2",
"gameover3",
"gameover4",
"gameover5"}
gameOverText.text = dead[Random.Range(0, dead.length)];
gameOver = true;
}
The issue is, when I go back to Unity, it tells me that there is a unexpected symbol "gameOverText". I must be missing a typo or a command or something, because it worked fine before I tried to add the random aspect. Being such a nooblet, I don't put it past myself to have missed something gigantic.
Answer by jpthek9 · Oct 27, 2014 at 02:17 AM
public void GameOver()
{
string[] dead = new List
{"gameover1",
"gameover2",
"gameover3",
"gameover4",
"gameover5"}**;**
gameOverText.text = dead[Random.Range(0, dead.length**-1**)];
gameOver = true;
}
I would like very much to say that this worked, and it looks like it will, but now it's decided that "List" and "length" are words from some ancient undecipherable language. Any thoughts on how I might fix that?
OH YEAH! Sorry it's a little bit late but make sure you type 'Using System.Collections.Generic' without quotes.
Okay, I did that, and "length" is working, but "list" is still red.
public void GameOver()
{
string<string> dead = new List<string>;
dead.Add("Gameover1"); dead.Add("Gameover2"; [and so on..]
gameOverText.text = dead[Random.Range(0, dead.length**-1**)];
gameOver = true;
}
Okay last shot at it...
Answer by Kiwasi · Oct 30, 2014 at 04:56 AM
You typically don't create arrays by declaring a list.
Try new string[] {...}