Impossible JavaScript

I don’t get it.
I had this array that contained two items before, and the indexed array will play the song. It worked just fine.
Now, I want to add more songs to the list. For some reason it wasn’t showing up/playing, so I decided to check the length of the array and turns out, it was the same as before (length was 2 though it should’ve been 3 since I had 3 items in the array).
I decided to see what would happen if I took one of the previous items out and kept the new one in to see if that would play. It didn’t, it somehow kept displaying and playing the old songs- though it’s impossible.

#pragma strict

var songList = ['Au5 - Snowblind','Rameses B - Transformations'];
var background = Texture();
var textMid = GUIText();
var song : AudioClip;
var buttonPress : AudioClip;
private var num = 0;

function OnGui()
{
    GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height), background);
}

function Start()
{
    textMid.text = songList[num];
    song = Resources.Load('Song Previews/' + songList[num].ToString(), AudioClip);
    audio.clip = song;
    audio.Play();
}

function Update()
{
    if(Input.GetKeyUp(KeyCode.LeftArrow))
    {
        if(num-1 < 0)
        {
            num = songList.Length-1;
        }
        textMid.text = songList[num];
        song = Resources.Load('Song Previews/' + songList[num].ToString(), AudioClip);
        audio.clip = song;
        audio.Play();
        Debug.Log(num + " = num, and the length of songlist: " + songList.Length);
    }
    if(Input.GetKeyUp(KeyCode.RightArrow))
    {
        if(num+1 == songList.Length)
        {
            num = 0;
        }
        textMid.text = songList[num];
        song = Resources.Load('Song Previews/' + songList[num].ToString(), AudioClip);
        audio.clip = song;
        audio.Play();
        Debug.Log(num + " = num, and the length of songlist: " + songList.Length);
    }
    if(Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.Return))
    {
        //game scene
        //Application.LoadLevel(songList[num].ToString());
    }
    if(Input.GetKeyUp(KeyCode.Escape) || Input.GetKeyUp(KeyCode.Backspace))
    {
        Application.LoadLevel("MainMenu");
    }
}

What currently loads is the first item in the list and a thing that no longer exists in the list instead of what I have as the second item in the list. Even if I add random things in just to see if it’ll pop up on the GUI, it won’t show and the length of the array remains 2.

Public variables get their values from the inspector, not your code (aside from the initial assignment).

–Eric

Sorry I am extremely stupid and I should just give up. The fix was that the values of num were never changing because I only checked what would happen if num-1 was less than zero. I never actually subtracted it after, and same goes for when I had to add.
God, I wish I didn’t spend a whole 30 minutes on this.
As for the array length not changing I just reset it in the inspector and threw all the textures and stuff back and set it private and seems to be fine now. Thanks.

Definitely shouldn’t ever give up; you found the bug and fixed it so that’s good progress. I’ve certainly spent longer on much stupider things…

–Eric

1 Like