Assigning String Value From Array in separate script sets value to null.

I have been trying all day to retrieve a string from an array over to an individual string variable in a separate script. (to load a scene with the same name.) This is the code i made to store the array of strings.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class LevelArrayStore : MonoBehaviour {
    
        public string[] LevelList;
    
    	// Use this for initialization
    	void Awake () {
            //Array length and data set in inspector.
            LevelList = new string[15];
    	}
    	
    }

Then this is the code for retrieving the string.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelOpen : MonoBehaviour {

     //Array index of string to be retrieved from array script.
    public int TargetLevelValue;

    //To store GameObject with array script
    public GameObject ArrayHolder;

    //String retrieved from array.
    string TargetLevelName;

	// Use this for initialization
	void Start () {

    }


    // Update is called once per frame
    void Update()
    {

        string TargetLevelName = ArrayHolder.GetComponent<LevelArrayStore>().LevelList[TargetLevelValue];

    }   

    //Called By Button
    public void LoadTargetLevel ()
    {
        SceneManager.LoadScene(TargetLevelName);
    }
}

Whenever I call “LoadTargetLevel” I get this error

Cannot load scene: Invalid scene name (empty string) and invalid build index -1
UnityEngine.SceneManagement.SceneManager:LoadScene(String)
LevelOpen:LoadTargetLevel() (at Assets/UI Scripts/LevelOpen.cs:29)
UnityEngine.EventSystems.EventSystem:Update()

Does Anyone Know What Is Going On?

In the function Update you are declaring a new variable TargetLevelName instead of assigning the value to the class member, remove “string” from Update so that it uses the class member TargetLevelName instead of a local variable.

Also I read “Array length and data set in inspector” in the comment of LevelArrayStore.Awake, but you’re reinitializing the array LevelList so it will overwrite what you set in the inspector as soon as the object is instantiated in the scene.

Also you should really cache the LevelArrayStore object in your Start function of the other script to boost the performance, instead of looking for it at every frame in Update.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelOpen : MonoBehaviour
{
    //Array index of string to be retrieved from array script.
    public int TargetLevelValue;

    //To store GameObject with array script
    public GameObject ArrayHolder;

    //String retrieved from array.
    string TargetLevelName;

    LevelArrayStore levelStore;

    // Use this for initialization
    void Start()
    {
        levelStore = ArrayHolder.GetComponent<LevelArrayStore>();
    }

    // Update is called once per frame
    void Update()
    {
        TargetLevelName = levelStore.LevelList[TargetLevelValue];
    }

    //Called By Button
    public void LoadTargetLevel()
    {
        SceneManager.LoadScene(TargetLevelName);
    }
}

don’t get the string, just use the build index, this will remove the need for the LevelArrayStore class and the script collecting the string. to check the build index go to: file, build settings. there you will find a scene in a list, just add the others in the order you want. to then load a level just use:

SceneManager.LoadScene(int build index);

unless for some reason it has to be a string, like the player types in stuff or something.