Hi,
I’m making a random generator in my game but my script isn’t working does anyone have any idea why?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace demo
{
class Program { }
static void main(string[] args)
{
var random = new random();
var list = new list<string> { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
int index = random.Next(list.count);
console.WriteLine(list[index]);
}
}
Its probably really simple but I haven’t been learning for that long and have only watched tutorials as a guide. Once the script is working I plan to change the numbers to different strings but I just wanted to keep it simple for now until its working.
Thanks 
I assume you’re watching a tutorial for some more generic kind of C# app, because that code is going to be useless in Unity. Unity scripts don’t use the “main” method. In Unity, people also generally don’t use C#'s Random class; Unity has its own Random class that is better optimized for games.
Even besides all that I don’t think that would work or even compile, because main() is outside of any class. Also List<> and Random are lowercase in your script, and capitalization matters. In the future, please copy and paste the actual errors you’re getting, as it will make it much easier to find issues.
See here for some sample code of how to do randomness in Unity.
Hi thanks, yes I was using pretty generic tutorials. Would I be able to change the script in your link to work for strings? Basically what I’m trying to achieve is when a button in pressed on screen the program randomly picks a sentence from list to form a story
Something like this:
var list = new List<string>{"1", "2", "3"};
int randomIndex = Random.Range(0, list.Count);
Debug.Log(list[randomIndex]);
Hi, sorry, at this point it must but really obvious that I haven’t had much experience with the program, I entered the code from your post
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace test
{ class random tester
{
var list = new List<string> { "1", "2", "3", "4", "5" };
int randomIndex = Random.Range(0, list.count);
debug.log(List[randomIndex]); }
I’m still getting errors and not being familiar with the coding side of things I can’t figure out why it isn’t working
Start with a new script that Unity generates (you’ve made a bunch of changes to where it’s easiest to just start over), then put my code inside the Start() { } function.
That works perfectly now thank you, one last question how would I get the generated string to appear in my main text block? Sorry for taking up your time on probably really basic questions
You add a UI canvas, and a GameObject under that, with a UI Text component attached. You put a reference to that component in the script you want to set the text from. You actually set the value of the text being output by accessing its text field.
public Text TextComponent;
void Start()
{
TextComponent.text = "Something blah blah";
}
The above linked tutorial probably explains it better.
Admittedly I’m still really confused, I have the random generation for my strings now and I have all the buttons and text boxes in place but now what I need is for the answer of the random generation to appear in my text box when my button is clicked. (I know how to get it on click I just don’t understand the code)