switching text not working while it should - what's wrong?

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

You’ll probably get more help if you post your code in the forum using code tags instead of directing people to an advertising covered external site, and don’t ask people to go watch a video of your problem.

Why are you incrementing or decrementing “index” twice in both of your button methods?

I would think you should reorganize those methods so you start by toggling off both the model and text object with the current index, then index++ or index-- just once, and then enable both the model and text object with the new index value. The way you’re currently doing it doesn’t make sense to me.

Ok I changed it. I was aware of that already but like I said in the thread I would change it when it works. Also, I don’t mean that you HAVE to watch my video, the video merely is a tool for those who want to see what’s not working visually.

I’d like to offer an alternative suggestion. Though I’ll admit I haven’t spotted the error in your code to cause your issue, you could try this:
Add a script to each planet game object. Say it’s called “Planet Info” (whatever). Add just one public string called “planetName” and fill that in.
In the script to change planets, only reference a singular text object, and when you change planets, get the component from the index like so…

levelList[index].SetActive(true);
planetNameText.text = levelList[index].GetComponent<PlanetInfo>().planetName;

This code checks index twice (same in the ++ version), which isn’t necessary:

index--;
if (index < 0)
    index = levelList.Length - 1;
if (index < 0)
    index = CanvasNameTag.Length - 1;

Ok, thanks for the reply!

Could you please send an example of that public string? I don’t know if I did it correctly by adding all the planet names to that one script.

all I meant was that a new script be placed on each planet game object. the new script has a public string variable…

public class PlanetInfo : MonoBehaviour {
  public string planetName;
}

Something like that.

But don’t you need to add the names in that script?

Well, sure… you put that script on each game object (planet) and then write the proper name for the planet in the inspector. :slight_smile:

So how exactly do i implement this is my existing code? I added it like you showed but it says that PlanetNameText.text doesn’t exist in that conext. Should I make one text object first as well?

Yes, you’d want to make just 1 text object and reference it in the script. Then, whichever planet is currently displayed, it will overwrite the ‘.text’ property to show the planet’s name.

hope that makes sense.