I’m trying to print the number in an array by modifying an int using a singleton.
not sure if this is the best method but i’m just experimenting.
i’m getting this error:
IndexOutOfRangeException: Index was outside the bounds of the array.
Health.Update () (at Assets/Health.cs:23)
public class GameManager : MonoBehaviour
{
public int Health = 0;
public static GameManager instace;
//int a = 5;
// Start is called before the first frame update
private void Awake()
{
instace = this;
}
// Update is called once per frame
void Update()
{
}
public void AddHealth(int health)
{
Health = Health + health;
}
}
public class Health : MonoBehaviour
{
public int[] arr = { 0, 2, 4, 7, 9 };
public int array;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
GameManager.instace.AddHealth(1);
//Debug.Log(GameManager.instace.Health);
int array = GameManager.instace.Health;
Debug.Log(arr[array]);
}
}
}