Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    #region Instance.
    public static GameManager Instance
    {
        get
        {
            if (instance == null)
            {
                GameObject go = new GameObject();
                go.AddComponent<GameManager>();
                instance = go.GetComponent<GameManager>();
            }
            return instance;
        }
    }
    private static GameManager instance;
    #endregion

    public Transform Player;
    public bool IsLevelCompleted;
    public GameObject[] levels;
    public int CurrentLevelNo;
    private void Awake()
    {
        instance = this;
    }

    private void Start()
    {
       
        CurrentLevelNo = PlayerPrefs.GetInt(Prefs.CurrentLevelNo, 0);
        Player = GameObject.FindGameObjectWithTag(Tags.Player).transform;
        Player = FindObjectOfType<RCC_CarControllerV3>().transform;
        Debug.Log(levels);
        levels[CurrentLevelNo].SetActive(true);
    }


    #region LevelPassed.
    public UnityEvent LevelPassedEvents;
    public UnityEvent LevelFailedEvents;
    public void FncLevelPassed(bool isPassed)
    {
        if (!IsLevelCompleted)
        {
            IsLevelCompleted = true;

            if (isPassed)
            {
                CurrentLevelNo += 1;
                if (CurrentLevelNo > levels.Length) CurrentLevelNo = levels.Length - 1;
                  //CurrentLevelNo = Mathf.Clamp(CurrentLevelNo, 0, levels.Length);
                PlayerPrefs.SetInt(Prefs.CurrentLevelNo, CurrentLevelNo);

                if (LevelPassedEvents != null && LevelPassedEvents.GetPersistentEventCount() > 0)
                {
                    LevelPassedEvents.Invoke();
                }
              
            }
            else
            {
                if (LevelFailedEvents != null && LevelFailedEvents.GetPersistentEventCount() > 0)
                {
                    LevelFailedEvents.Invoke();
                }
            }
        }
    }
    #endregion
}


What I have tried:

Dear all,

I have a question I got below error message…

Assign : Index was outside the bounds of the array.

How can I fix this? sorry for vague question but with my capacity it is the best I could describe in detail…
Posted
Updated 20-Nov-19 0:53am
v2

1 solution

The error message is pretty straightforward.
Your levels collection doesn't contain any element at the index CurrentLevelNo.
You need to debug your code to find out what is in your collection and what the index is.
 
Share this answer
 
Comments
Member 14662029 20-Nov-19 8:29am    
if i have put the levels into array and the currentlevelno goes to length of array so why they have give the error index was outside bound of array .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900