I am trying to set the buttons in the Level Selector automatically but for any reason the Buttons won’t create. Any idea on what I did wrong? The code that I use is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Levels : MonoBehaviour
{
Button locked;
Button unlocked;
Rect rc;
private GUIStyle guiStyle = new GUIStyle();
public Texture lockedTexture;
public Texture unlockedTexture;
public int levelAmount;
int level = 1;
void Start()
{
}
void Update()
{
}
void OnGUI()
{
for (int i = 0; i <= levelAmount; i++)
{
print(getRect(i));
if (i <= level)
{ // entsperrt
GUI.Button(getRect(i), unlockedTexture);
}
else
{ // nicht ensperrt
GUI.Button(getRect(i), lockedTexture);
}
}
}
Rect getRect(int currentLevel)
{
int Height = currentLevel * 210;
int Width = currentLevel * 210;
int page = 0;
int temp = (currentLevel - page) / 8 ;
if(temp >= 0 && temp < 1)
{
rc = new Rect(-752 + Width, 296 + Height, 10, 10);
}
else if(temp >= 1 && temp < 2)
{
rc = new Rect(-752 + Width, 296 + Height, 10, 10);
}
else if (temp >= 2 && temp < 3)
{
rc = new Rect(-752 + Width, 296 + Height, 10, 10);
}
else if (temp >= 3 && temp < 4)
{
rc = new Rect(-752 + Width, 296 + Height, 10, 10);
}
else if(temp > 4)
{
page = currentLevel;
}
return rc;
}
}