Greetings everyone,
So I have this script for the level selection in my own little game. To achieve that I can switch between levels using buttons I followed the character selection tutorial by N3K on YouTube. This worked and all I needed now for in the level selection scene were nametags. So I though why don’t I do it the same way as with levels. It didn’t work, and after a few adjustments suggested on N3K’s Discord I came to the script that is attached in this thread at the bottom. This also didn’t work. It toggled of all nametags but the first one (index 0) but when I clicked on the right button, I got to see the next level but not the next nametag. So I went back to Discord to dibble check my script and they said it should work fine. It doesn’t. To make it clearer what works and what doesn’t I made a video:
.
All I know is basic scripting and at the moment my script is a little mess. I will tidy it up as soon as I get everything working. So please do not tell me that my script looks like a mess, believe me, I know. Aside from that I am just starting with c#, and all the code is pretty basic. Please do not tell me that there are better, other ways to achieve what works because I know. But I understand what I did here and that’s fine, I’ll improve step by step.
Please take a look at my script and video to see if you can find why it is not working. I would really appreciate it if you copy-paste the script and edit it so it works because I can’t really understand descriptions yet: I don’t have enough knowledge yet to do so.
Script:
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LevelSelection : MonoBehaviour
{
public GameObject[] levelList;
private Text[] CanvasNameTag;
private int index;
private void Start()
{
index = PlayerPrefs.GetInt("levelSelected");
levelList = new GameObject[transform.childCount];
//Fill the array with our levels
for (int i = 0; i < transform.childCount; i++)
levelList[i] = transform.GetChild(i).gameObject;
CanvasNameTag = transform.GetComponentsInChildren<Text>();
//We toggle of their renderer
foreach (GameObject go in levelList)
go.SetActive(false);
foreach (Text go in CanvasNameTag)
go.gameObject.SetActive(false);
//We toggle on selected level
if (levelList[index])
levelList[index].SetActive(true);
if (CanvasNameTag[index])
CanvasNameTag[index].gameObject.SetActive(true);
}
public void OnSelectClick()
{
PlayerPrefs.SetInt("LevelSelected", index);
SceneManager.LoadScene("Menu");
}
public void ToggleLeft()
{
//Toggle off the current model
levelList[index].SetActive(false);
CanvasNameTag[index].gameObject.SetActive(false);
index--;//index -=; index = index - 1;
if (index < 0)
index = levelList.Length - 1;
if (index < 0)
index = CanvasNameTag.Length - 1;
//Toggle on the new model
levelList[index].SetActive(true);
CanvasNameTag[index].gameObject.SetActive(true);
}
public void ToggleRight()
{
//Toggle off the current model
levelList[index].SetActive(false);
CanvasNameTag[index].gameObject.SetActive(false);
index++;//index -=; index = index - 1;
if (index == levelList.Length)
index = 0;
if (index == CanvasNameTag.Length)
index = 0;
//Toggle on the new model
levelList[index].SetActive(true);
CanvasNameTag[index].gameObject.SetActive(true);
}
}
I would like to thank you in advance! I really appreciate your help!
Kind regards,
Dave