- Home /
Short Break Between Sequence of Colors
In my really simple simon says type game, there is a sequence of colors the player has to match. The only problem is when the computer decides if the player answers correctly, it instantly goes to the next sequence. I would like to have the game wait for a second before going on to the next sequence, but I don't know how. If anyone knows why, I would appreciate it if you let me know. My script:
public void ColorPressed(int whichButton){
if (activeSequence [inputInSequence] == whichButton) {
if (gameActive) {
Debug.Log ("Correct");
waitBetweenLevels -= Time.deltaTime;
inputInSequence++;
}
if (inputInSequence >= activeSequence.Count) {
if (activeSequence.Count > PlayerPrefs.GetInt ("EasyHighScore")) {
PlayerPrefs.SetInt ("EasyHighScore", activeSequence.Count);
}
scoreText.text = "Score: " + activeSequence.Count + "- High Score: " + PlayerPrefs.GetInt ("EasyHighScore");
posInSequence = 0;
inputInSequence = 0;
colorSelect = Random.Range (0, colors.Length);
activeSequence.Add (colorSelect);
colors [activeSequence [posInSequence]].color = new Color (colors [activeSequence [posInSequence]].color.r, colors [activeSequence [posInSequence]].color.g, colors [activeSequence [posInSequence]].color.b, 1f);
buttonSounds [activeSequence [posInSequence]].Play ();
litCounter = lit;
shouldBeLit = true;
gameActive = false;
correct.Play ();
//Break Here
}
Answer by elenzil · Sep 23, 2016 at 10:55 PM
your example has a ton of code, so i haven't read it. (in general folks are more likely to read code you post when it's the minimal amount of code which demonstrates the problem you're having)
but this sounds like a good use for a coroutine. coroutines are perfect for sequences of things with pauses included.
here's an example.
void doSomeStuff() {
// this is a normal routine, and will return like normal
StartCoroutine(myCR("hello"));
// execution flows straight through to here after _starting_ the coroutine.
// ie, in this function, execution does not wait for the coroutine to finish.
}
IEnumerator myCR(string exampleParameter) {
Debug.log("here's a line");
yield return new WaitForSeconds(2);
Debug.log("here's a line 2 seconds later. also here's that parameter: " + exampleParameter);
}
Thanks! I tried doing something like this, but unless I was doing something, it gave me an error. Is it possible that the error was caused because I'm trying to put it in my public void ColorePressed() ? And if so, how you go about doing it?
I also shortened the code to the area I think is the most important to this question
good job shortening. what error ?
try getting a basic coroutine like the one i sketched above working, and then changing it gradually into what you want, and see at what point you run into a problem.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Literally just started coding in c#, having error cs1729 1 Answer