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?