- Home /
What is a simple way to load a random scene from an array? C#
I'm trying to figure out how to load a random scene from an array of scenes using C#
Answer by Glurth · Feb 06, 2015 at 05:42 PM
This function takes a STRING as a parameter, http://docs.unity3d.com/ScriptReference/Application.LoadLevel.html
It also takes an index, but not sure how to use that (though it would probably eliminate the need for the string array described below.)
So, all you need to do is create an array of say.. 10 strings, with the names of your scenes, as displayed in the editor. (this is un-compiled code, may have some errors, but you'll get the idea)
string[] sceneArray= {"openeing", "scene1", "closing".... };
Then you can use Random.Range, to select one
string sceneToRun= sceneArray[Random.Range(0,sceneArray.Length-1)];
Then launch it. Application.LoadLevel(sceneToRun)
;
I tried this but I get errors? //Scene Array string[] sceneArray= {"RadarScreen", "RadarScreen02", ""};
string sceneToRun= sceneArray[Random.Range(0,sceneArray.Length-1)];
Application.LoadLevel(sceneToRun);
I didn't test it out myself, what error do you get? Do you have a scene named "" ? Probabaly not, so I'd change that to a valid name, or remove it. also, you can test by putting the name DIRECTLY in your LoadLevel call, an ignore the array stuff, till you get this part working.
2 errors. I did put actual names of the scene in the array though? Assets/Scripts/SystGuidanceRadarButton.cs(12,30): error CS1519: Unexpected symbol (' in class, struct, or interface member declaration and Assets/Scripts/SystGuidanceRadarButton.cs(12,41): error CS1519: Unexpected symbol
)' in class, struct, or interface member declaration
oh, compiler errors? I assumed you would be able to handle those. I see below you have a "code guy".. he should be able to fix those lickety-split: just send him a link to this question.
(Or you can google those errors to try and resolve them yourself. I'm sorry bud, just not into $$anonymous$$ching c#, at least not on this site.)
Glurth: using scene numbers is easy. Same idea, but: int[] Snums={3,7,9,12};
then LoadLevel(Snums[i]);
.
Your answer
Follow this Question
Related Questions
Multiplayer Level Loading 1 Answer
How to load an inventory array? 1 Answer
How to make a loading screen? 2 Answers
Loadingscreen and play Level when initialized completely 1 Answer
Why does Application.LoadLevelAsync cause a huge spike? 2 Answers