- Home /
how to randomly pick a string from an array
I am really new to scripting and I want to create a list of names and then when I start the game the program picks a random name and shows it to me. I prefer in a GUI Label. this is the code I tried:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class random : MonoBehaviour {
string[] names = new List { "player1", "player2", "player3", "player4", "player5" };
int index = random.Next(strings.Count);
var name = names[index];
void OnGUI ()
{
GUILayout.Label (name);
}
}`
but it gave me this error:
The contextual keyword `var' may only appear within a local variable declaration
Please help me
Hi
I am currently have the same issue.
I have used the code from above, but are getting this error: A field initializer cannot reference the non-stratic field, method, or property texts
The code:
string[] texts = new string[] { "player1", "player2", "player3", "player4", "player5" };
string name = texts[Random.Range(0, names.Length - 1)];
For those people who may see this and wonder the answer.
The error is thrown because 'name' is referencing a property that hasn't been created yet. Properties initializers (texts) run after field initializers (name). In a nutshell, you can't initialise a variable with any other class level value, unless it's static.
Assign in a constructor, ins$$anonymous$$d:
string[] texts = new string[] { "player1", "player2", "player3", "player4", "player5" };
string name = null;
public $$anonymous$$yClass()
{
name = texts[Random.Range(0, names.Length - 1)];
}
if you need everything in the array use
array.Lenght;
if you dont need the last object in the array
array.Lenght - 1;
Answer by captaincrunch80 · Jul 17, 2012 at 05:01 PM
Your class random has no such function. It has only the variables and methods inherited from MonoBehaviour.
You should not use var in C#. Use the type string in this case.
What you want is the class method Range of Class Random.
string name = names[Random.Range(0, names.length -1)];
Here are the docs for Random.Range http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html
Also you should not call your class random.
Name it after what it is in a higher abstraction and have a capital letter at the beginning to stick to name conventions.
It picks a player by random. So the highest abstraction of that would be something like class RandomPlayerSelection.
But I believe it is just a test class, so its purpose will change later, or it is throw away code. Nevertheless, random is not a good class name for this and there already exits a class called Random. So coding errors are guaranteed.
It should be names.Length with capital "L". If anyone have trouble with this.
You should not subtract 1 from the length. The Range function operates with the $$anonymous$$ inclusive and the max exclusive.
Answer by Graham-Dunnett · Jul 17, 2012 at 04:01 PM
var is a keyword that Javascript uses to introduce a new variable declaration. C# does not use this keyword. Instead do something like:
string name = names[index];
Note that you have some code outside of any function. That's bad. Instead, put that code inside the Start() function. Unity will call Start() when your game object that has this script attached is created. The name variable is needed by your OnGUI() so declare it outside of Start and OnGUI.
Well, c# does have a var keyword. Don't use it until you know what it's for. It's better to provide the type for your variables.
Incorrect.. C# type inference has been around C# 3.0. And you can use the var keyword safely in C# in Unity. With certain rules of course.. http://stackoverflow.com/questions/479883/how-good-is-the-c-sharp-type-inference
I got more errors now 1 red error:`random' does not contain a definition for `Next' and two yellow which are also about random.name
Answer by Meltdown · Jul 17, 2012 at 06:05 PM
You have lots of errors in your script. I'd suggest doing a C# beginners tutorial to understand what is going on, we can't really help you with such basics all the time. I'd suggest getting a 1 month subscription to lynda.com and doing the C# essentials training video course.
Basically you need to change these lines..
int index = random.Next(strings.Count);
var name = names[index];
to
int index = Random.Range(0, names.Length - 1);
string name = names[index];
You are right about beginners tutorials still thx for help
still needed to change something but got it working now thx again
Your answer
Follow this Question
Related Questions
How to create a random array avoiding repeat same value 1 Answer
Unity Shutdown 1 Answer
Assigning array element to variable 1 Answer