Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

How to make a naming script for buttons?

Discussion in 'Scripting' started by Toxvigm, May 7, 2020.

  1. Toxvigm

    Toxvigm

    Joined:
    May 7, 2020
    Posts:
    2
    Hi, I'm working on a quizz game as a little project for my coworkers. I'm not an expert in coding.

    Here is my issue : I was getting tired of always having to rewrite text in my levels button and make it all centered and nice. So I made this little simple script to name the buttons and made sure everything was alright with my prefabs.


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class LevelSelectorUI : MonoBehaviour
    4. {
    5.     [SerializeField] private LevelButton buttonPrefab = null;
    6.  
    7.     private void Start() {
    8.         for (int i = 0; i < LevelManager.instance.AllLevels.Length; i++) {
    9.             var lb = Instantiate(buttonPrefab, transform);
    10.             lb.Setup(i+1, LevelManager.instance.AllLevels[i]);
    11.         }
    12.     }
    13. }
    14.  
    Now my issue is that I'm planning on adding "Bonus levels" in my game. So for example after level 4, you would have a "hidden" level 4.5.

    I was thinking of making a script that instead of adding +1, goes through a list of "names and numbers" that I would just simply edit. Every new instance of the button would have the next name or number. But I'm a complete beginner at coding and can't seem to wrap my head around it...

    For example the list go go as follow :
    1
    2
    3
    4
    4.5
    5
    5.5
    6
    Boss
    etc...

    Any help is appreciated
     
  2. ashkanaral

    ashkanaral

    Joined:
    Sep 3, 2018
    Posts:
    22
    Try using an if statement when it is needed such as the 4.5 or 5.5