Issue With Coding Rhythm Game

I’m using the following code to create notes for my rhythm game.

==========================================================

List whichnote = new List() {1,2,3,4,5,6,7,8,9,10};

public int notemark = 1;

public Transform qObj;

public float xPos;

public static int Combo = 0;

public static float totalscore = 0;

public float bpm = 175;

public float crotchet;

// Use this for initialization
void Start () {

crotchet = 60 / bpm;
}

// Update is called once per frame
void update()
{
if (Conductor.songposition > crotchet * notemark) //songposition =(float)(AudioSettings.dspTime)
{
StartCoroutine(Createnote());

}

}

IEnumerator Createnote()
{
yield return new WaitForSeconds(0);
if (whichnote [notemark] == 1)
{
xPos = -50f;
}
if (whichnote [notemark] == 2)
{
xPos = -13f;
}
if (whichnote [notemark] == 3)
{
xPos = -12f;
}
if (whichnote [notemark] == 4)
{
xPos = -11f;
}
if (whichnote [notemark] == 5)
{
xPos = -10f;
}
if (whichnote [notemark] == 6)
{
xPos = -9f;
}
if (whichnote [notemark] == 7)
{
xPos = -8f;
}
if (whichnote [notemark] == 8)
{
xPos = -7f;
}
if (whichnote [notemark] == 9)
{
xPos = -6f;
}
if (whichnote [notemark] == 10)
{
xPos = -5f;
}
if (whichnote [notemark] == 11)
{
xPos = -4f;
}
if (whichnote [notemark] == 12)
{
xPos = -3f;
}
if (whichnote [notemark] == 13)
{
xPos = -2f;
}
if (whichnote [notemark] == 14)
{
xPos = -1f;
}
if (whichnote [notemark] == 15)
{
xPos = 0f;
}
if (whichnote [notemark] == 16)
{
xPos = 1f;
}
if (whichnote [notemark] == 17)
{
xPos = 2f;
}
if (whichnote [notemark] == 18)
{
xPos = 3f;
}
if (whichnote [notemark] == 19)
{
xPos = 4f;
}
if (whichnote [notemark] == 20)
{
xPos = 5f;
}
if (whichnote [notemark] == 21)
{
xPos = 6f;
}
if (whichnote [notemark] == 22)
{
xPos = 7f;
}
if (whichnote [notemark] == 23)
{
xPos = 8f;
}
if (whichnote [notemark] == 24)
{
xPos = 9f;
}
if (whichnote [notemark] == 25)
{
xPos = 10f;
}
if (whichnote [notemark] == 26)
{
xPos = 11f;
}
if (whichnote [notemark] == 27)
{
xPos = 12f;
}
notemark += 1;
Instantiate(qObj, new Vector3(xPos, 7f, 12f), qObj.rotation);

}

}

===============================================================================

What should happen is that a note will spawn whenever the songposition is greater than crotchet (about 0.3) multiplied by note mark (1). this means the first note in the list will spawn after 0.3 seconds. Notemark will then increase by 1, which means that the second note in the list will spawn at 0.6 seconds.

What actually happens is that a note spawns every single time the game updates, and I can’t figure out why this happens. Is there something i’m missing?

3174254–241904–gm.cs (3.29 KB)

First use this so we can read your code Using code tags properly - Unity Engine - Unity Discussions

List<float> whichnote = new List<float>() {1,2,3,4,5,6,7,8,9,10};

public int notemark = 1;

public Transform qObj;

public float xPos;

public static int Combo = 0;

public static float totalscore = 0;

public float bpm = 175;

public float crotchet;









// Use this for initialization
void Start () {

crotchet = 60 / bpm;
}

// Update is called once per frame
void update()
{
if (Conductor.songposition > crotchet * notemark) //songposition =(float)(AudioSettings.dspTime)
{
StartCoroutine(Createnote());


}



}

IEnumerator Createnote()
{
yield return new WaitForSeconds(0);
if (whichnote [notemark] == 1)
{
xPos = -50f;
}
if (whichnote [notemark] == 2)
{
xPos = -13f;
}
if (whichnote [notemark] == 3)
{
xPos = -12f;
}
if (whichnote [notemark] == 4)
{
xPos = -11f;
}
if (whichnote [notemark] == 5)
{
xPos = -10f;
}
if (whichnote [notemark] == 6)
{
xPos = -9f;
}
if (whichnote [notemark] == 7)
{
xPos = -8f;
}
if (whichnote [notemark] == 8)
{
xPos = -7f;
}
if (whichnote [notemark] == 9)
{
xPos = -6f;
}
if (whichnote [notemark] == 10)
{
xPos = -5f;
}
if (whichnote [notemark] == 11)
{
xPos = -4f;
}
if (whichnote [notemark] == 12)
{
xPos = -3f;
}
if (whichnote [notemark] == 13)
{
xPos = -2f;
}
if (whichnote [notemark] == 14)
{
xPos = -1f;
}
if (whichnote [notemark] == 15)
{
xPos = 0f;
}
if (whichnote [notemark] == 16)
{
xPos = 1f;
}
if (whichnote [notemark] == 17)
{
xPos = 2f;
}
if (whichnote [notemark] == 18)
{
xPos = 3f;
}
if (whichnote [notemark] == 19)
{
xPos = 4f;
}
if (whichnote [notemark] == 20)
{
xPos = 5f;
}
if (whichnote [notemark] == 21)
{
xPos = 6f;
}
if (whichnote [notemark] == 22)
{
xPos = 7f;
}
if (whichnote [notemark] == 23)
{
xPos = 8f;
}
if (whichnote [notemark] == 24)
{
xPos = 9f;
}
if (whichnote [notemark] == 25)
{
xPos = 10f;
}
if (whichnote [notemark] == 26)
{
xPos = 11f;
}
if (whichnote [notemark] == 27)
{
xPos = 12f;
}
notemark += 1;
Instantiate(qObj, new Vector3(xPos, 7f, 12f), qObj.rotation);





}




}

Is it due to the yield return new WaitForSeconds(0);

Should it read yield return new WaitForSeconds(0.0f);
or yield return new WaitForSeconds(0.001f);

at first glance I thought your StartCoroutine(Createnote()); was outside the update but it’s not…

also could it be possible for your if to be converted to a Switch/case statement???

also that update will continue to loop, maybe could set a on and off bound to limit it???