If statement not working.

Here is my script

function NoteDecision(n :String) {
	Debug.Log(n);
	if (n == "G") {
        Debug.Log("Hi")
		Instantiate(GreenNote, SGreenNote.position, SGreenNote.rotation);
	}
	if (n == "R") {
		Instantiate(RedNote, SRedNote.position, SRedNote.rotation);
	}
	if (n == "Y") {
		Instantiate(YellowNote, SYellowNote.position, SYellowNote.rotation);
	}
	if (n == "B") {
		Instantiate(BlueNote, SBlueNote.position, SBlueNote.rotation);
	}
	if (n == "O") {
		Instantiate(OrangeNote, SOrangeNote.position, SOrangeNote.rotation);
	}
}

Now, in the above script, I have 5 if statements. You can see that I debug n, and although it does match up, nothing happens… I tried to create green notes but Hi was never printed and no green note was instantiated, even though n was set to G… Any help?

David

It’s hard to tell why it doesn’t work, it could be due to case sensitivity or the string having a blank before or after the letter.

You could try an enumeration instead, which is quicker than comparing strings (win-win).

enum NOTES {
    Green,
    Red,
    Yellow,
    Blue,
    Orange
}

function NoteDecision (n : NOTES) {
    switch (n) {
        case NOTES.Green: Instantiate(GreenNote, SGreenNote.position, SGreenNote.rotation); break;
        case NOTES.Red: Instantiate(RedNote, SRedNote.position, SRedNote.rotation); break;
        case NOTES.Yellow: Instantiate(YellowNote, SYellowNote.position, SYellowNote.rotation); break;
        case NOTES.Blue: Instantiate(BlueNote, SBlueNote.position, SBlueNote.rotation); break;
        case NOTES.Orange: Instantiate(OrangeNote, SOrangeNote.position, SOrangeNote.rotation); break;
        default: Debug.Log("Case statement is missing for the NOTES enum!"); break;
    }
}

Calling the function would be done like this: NoteDecision(NOTES.Green);. Declaring a variable with a note would be done like this: greenNote : NOTES = NOTES.Green;.

Not sure about How Javascript String compression works. try to use String.Equals(string, string )

Both answers work for me, but Ill mark the enumeration one right because im using it. Thanks!