Search Unity

array index out of range exception

Discussion in 'Scripting' started by shahroziub, Sep 8, 2019.

  1. shahroziub

    shahroziub

    Joined:
    Sep 8, 2019
    Posts:
    21
    Anyone Please help
    array index out of range exception


    (MenuState == E_MenuState.ChaptersSubmenu)
    {
    if(id == BackID)
    {
    Back.GetComponent<Animation>().Play("press");
    StartCoroutine(StartFromChapters2MainMenu());
    }
    else
    {
    int index = id - ChapterID[0];
    Chapter[index].GetComponent<Animation>().Play("press");
    StartCoroutine(StartChapter(index));
    }
    }
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    It means you are attempting to access an array at an index which is not within range. In your case it would be the Chapter array.

    place a Debug.Log("Chapter Index = " + index); after the int and see at which index you are trying to access it.

    Also remember that arrays are 0 based so if it only has one element, you would access that element at index 0, not 1. If it has 4 elements, the 4th would be accessed at an index of 3.
     
    shahroziub likes this.
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Hi and welcome.
    Please use code tags to post code examples, as it makes reading them much more easy. First sticky on this subforum. Generally speaking it's also helpful if you posted the actual error message with a note for which line throws the error. As WarmedxMints said, in your case it's most likely Chapter[index], which gets accessed at some index outside of its bounds. For example, if you have an array of size 10, then it has elements on indices 0 to 9, so accessing any index >= 10 results in an out of bounds exception.
     
    shahroziub likes this.
  4. shahroziub

    shahroziub

    Joined:
    Sep 8, 2019
    Posts:
    21
    Here's complete Update function i also do debug.log it shows -10 index
    even i make total 8 index in unity inspector


    Code (CSharp):
    1. public void Update()
    2.     {
    3.         //Debug.Log("hackyhack,samurai ||");
    4. //        Debug.Log("main menu updating");
    5.         if(Input.touchCount == 0)
    6.             return;
    7.  
    8.         Touch t = Input.touches[0];
    9.  
    10.         if(t.phase != TouchPhase.Ended)
    11.             return;
    12.  
    13.         RaycastHit hit;
    14.         if (Physics.Raycast(Camera.main.ScreenPointToRay(t.position), out hit, Mathf.Infinity) == false)
    15.             return;
    16.  
    17.         int id = hit.collider.gameObject.layer;
    18.         AudioSource.PlayClipAtPoint(SoundsButton[Random.Range(0, SoundsButton.Length)], Position);
    19.      
    20.         if (MenuState == E_MenuState.MainMenu)
    21.         {
    22.             switch(id)
    23.             {
    24.                 case SinglePlayerID:
    25.                     SinglePlayer.GetComponent<Animation>().Play("MainButtonsPress");
    26.                     StartCoroutine(StartSinglePlayerSubMenu());
    27. //                admobdemo.Showinter();
    28.                     break;
    29.                 case DojoID:
    30.                     Dojo.GetComponent<Animation>().Play("MainButtonsPress");
    31.                     StartCoroutine(StartDojoSubMenu());
    32.                     break;
    33.                 case ChaptersID:
    34.                     Chapters.GetComponent<Animation>().Play("MainButtonsPress");
    35.                     StartCoroutine(StartChaptersSubMenu());
    36.                     break;
    37.                 case LeaderboardsID:
    38.                     Leaderboards.GetComponent<Animation>().Play("AandL_press");
    39.                     StartCoroutine(StartLeaderboards());
    40.                     break;
    41.                 case AchievementsID:
    42.                     Achievements.GetComponent<Animation>().Play("AandL_press");
    43.                     StartCoroutine(StartAchievements());
    44.                     break;
    45.                 case GameCenterID:
    46.                     //GameCenter.animation.Play("AandL_press");
    47.                 Application.OpenURL("");
    48.                     break;
    49.                 case HelpID:
    50.                     Help.GetComponent<Animation>().Play("MainButtonsPress");
    51.                     StartCoroutine(StartTutorial());
    52.             //    Advertisement.Show();
    53.                     break;
    54.                 case MoreGamesID:
    55.               //      MoreGames.GetComponent<Animation>().Play("MainButtonsPress");
    56.                 //Application.OpenURL("https://play.google.com/store/apps/dev?id=8072288438760313224");
    57.                     break;
    58.             }
    59.             //if(id != GameCenterID) {
    60.             //    GameCenter.animation.Play("MainButtonsOut");
    61.             //}
    62.         }
    63.         else if(MenuState == E_MenuState.DojoSubmenu)
    64.         {
    65.             switch(id)
    66.             {
    67.                 case DojoResumeID:
    68.                     DojoResume.GetComponent<Animation>().Play("press");
    69.                     StartCoroutine(StartResumeDojo());
    70.                     break;
    71.                 case DojoID:
    72.                     DojoNew.GetComponent<Animation>().Play("press");
    73.                     StartCoroutine(StartNewDojo());
    74.                     break;
    75.                 case BackID:
    76.                     Back.GetComponent<Animation>().Play("press");
    77.                     StartCoroutine(StartFromDojo2MainMenu());
    78.                     break;
    79.             }
    80.         }
    81.         else if (MenuState == E_MenuState.GameSubmenu)
    82.         {
    83.             switch(id)
    84.             {
    85.                 case GameResumeID:
    86.                     GameResume.GetComponent<Animation>().Play("press");
    87.                     StartCoroutine(StartResumeGame());
    88.                     break;
    89.                 case GameEasyID:
    90.                     GameEasy.GetComponent<Animation>().Play("press");
    91.                     StartCoroutine(StartNewEasyGame());
    92.                     break;
    93.                 case GameNormalID:
    94.                     GameNormal.GetComponent<Animation>().Play("press");
    95.                     StartCoroutine(StartNewNormalGame());
    96.                     break;
    97.                 case GameHardID:
    98.                     GameHard.GetComponent<Animation>().Play("press");
    99.                     StartCoroutine(StartNewHardGame());
    100.                     break;
    101.                 case BackID:
    102.                     Back.GetComponent<Animation>().Play("press");
    103.                     StartCoroutine(StartFromGame2MainMenu());
    104.                     break;
    105.             }
    106.         }
    107.         else if (MenuState == E_MenuState.ChaptersSubmenu)
    108.         {
    109.             if(id == BackID)
    110.             {
    111.                 Back.GetComponent<Animation>().Play("press");
    112.                 StartCoroutine(StartFromChapters2MainMenu());
    113.             }
    114.             else
    115.             {
    116.                 int  index =  id - ChapterID[0];
    117.                  Debug.Log("Chapter Index = " + index);
    118.              Chapter[index].GetComponent<Animation>().Play("press");
    119.                 StartCoroutine(StartChapter(index));
    120.             }
    121.         }
    122.     }

    upload_2019-9-9_17-22-19.png

    upload_2019-9-9_17-22-37.png
     
    Last edited: Sep 9, 2019
  5. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    I'm not entirely sure what it is you are doing, but effectively you are calculating the index by subtracting ChapterID[0] from the id of a layer, if i got this right. So assuming ChapterID[0] contains something like 10 and the layer id is 0, then it makes sense for your index to be -10, but accessing something at -10 does not make sense.

    First of all, you may wanna adjust your Debug.Log to print id and ChapterID[0] together with the calculated index. That way you can see which values result in the index you calculate. After that you need to think about what it is you are trying to do, so you can make sense of what's going wrong.
    If you still have troubles after going through the above, try explaining what it is you want to do. Without further information it's hard giving advice, but i believe you should be able to find the mistake when you think about what you are trying to calculate vs what it is you are actually calculating.
     
  6. shahroziub

    shahroziub

    Joined:
    Sep 8, 2019
    Posts:
    21
    after debug it shows
    chapter Index = -10
    ChapterID= 20
    id=10
    when i do debug on Previous chapter that work fine
    chapter index = 6
    ChapterID= 20
    id=26
     
    Last edited: Sep 9, 2019
  7. shahroziub

    shahroziub

    Joined:
    Sep 8, 2019
    Posts:
    21
    private int[] ChapterID ={20,21,22,23,24,25,26,27};
     
  8. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    As you can see, and as i stated above, when your id is smaller than ChapterID than you get a negative number for your index, which inevitably results in an array out of bounds error. But i dont know what the intended behavior is supposed to be, so you really have to think about:
    What do you want to do? What index do you expect with what inputs? Why does this not always work? What can you do about it?
     
    shahroziub likes this.
  9. shahroziub

    shahroziub

    Joined:
    Sep 8, 2019
    Posts:
    21
    actually i want to add chapters previous i have 7 chapters in game that work fine now i try to add 8 chapter
    i want to access ChapterIndex 7 but i try it gives me exception i'm stuck here from a week :(
    if
    chapter index = 7
    ChapterID= 20
    id=27
    then i belive it work fine
     
  10. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    So for the first pairs of id + ChapterID it works, like in the example 26-20=6, with 6 being the desired result, but with the newly added 8th chapter it does not work, since it it results in 10-20=-10, which is obviously wrong. Then i gotta ask: what did you do differently? Why are all the other ids so high, but your newly added one results in a number lower than the ChapterID? May be some problem with how you set it up.

    And if the index is supposed to be some combination of the id and the ChapterID, cant you store the values you are trying to access with this new index in a 2-dimensional jagged/multidimensional array instead and access it using [id, ChapterID] without any calculations?

    This seems like a very simple problem that should have an elegant, efficient solution to it.. if i just understood what exactly the problem is and why you are doing things as you do. It's easy to see that you are trying to access some Chapter based on an index. It's not easy to see how the chosen Chapter is decided on, or what meaning is behind the (index) calculation. It's not easy to see why your ChapterIDs start with 20. It's not easy (for me at least) to see what role the "id", which is some layer, plays in all of this. If you could clarify how these things are related, i can give you a proper solution. But as it is right now, half your code is a black box to me, so i dont know why or how these things are supposed to work, which is kind of important knowledge.
     
  11. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Why is chapter index equal to -10? Seems like you have a logic error. I would suggest that you manually step through your loop logic, "be the computer"!
     
  12. shahroziub

    shahroziub

    Joined:
    Sep 8, 2019
    Posts:
    21
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class MainMenu : MonoBehaviour {
    6.  
    7.     // 游戏名
    8. //    public string ofname;
    9.     // 游戏id
    10. //    public string ofid;
    11.     // 游戏key
    12. //    public string ofkey;
    13.     // 游戏secret
    14. //    public string ofsecret;
    15.     // æ¸ é“æ ‡è¯†
    16.     //public string appstore;
    17.  
    18.     public static OpenFeintFacade openFeint;
    19.  
    20.     enum E_MenuState
    21.     {
    22.         Busy,
    23.         MainMenu,
    24.         GameSubmenu,
    25.         DojoSubmenu,
    26.         ChaptersSubmenu,
    27.     }
    28.  
    29.     public GameObject MainLine;
    30.     public GameObject MainBackground;
    31.     public GameObject SinglePlayer;
    32.     public GameObject Dojo;
    33.     public GameObject Chapters;
    34.     public GameObject Help;
    35.     public GameObject MoreGames;
    36.  
    37.     public GameObject ALBackground;
    38.     public GameObject Achievements;
    39.     public GameObject Leaderboards;
    40.     public GameObject GameCenter;
    41.  
    42.     public GameObject GameTitle;
    43.     public GameObject GameBackground;
    44.     public GameObject GameResume;
    45.     public GameObject GameEasy;
    46.     public GameObject GameNormal;
    47.     public GameObject GameHard;
    48.  
    49.     public GameObject DojoTitle;
    50.     public GameObject DojoBackground;
    51.     public GameObject DojoResume;
    52.     public GameObject DojoNew;
    53.  
    54.     public GameObject ChaptersTitle;
    55.     public GameObject ChaptersBackground;
    56.  
    57.     public GameObject ChaptersLocked;
    58.     public GameObject[] Chapter = new GameObject[8];
    59.  
    60.     public GameObject Back;
    61.     public GameObject Loading;
    62.  
    63.     public AudioClip[] SoundsButton;
    64.     public AudioClip SoundMainMenuIn;
    65.     public AudioClip SoundMainMenuOut;
    66.     public AudioClip SoundSubMenuIn;
    67.     public AudioClip SoundSubMenuOut;
    68.  
    69.     private const int AchievementsID = 18;
    70.     private const int GameCenterID = 19;
    71.     private const int SinglePlayerID = 20;
    72.     private const int DojoID = 21;
    73.     private const int ChaptersID = 22;
    74.     private const int LeaderboardsID = 23;
    75.     private const int HelpID = 24;
    76.     private const int MoreGamesID = 25;
    77.  
    78.     private const int GameResumeID = 20;
    79.     private const int GameNormalID = 21;
    80.     private const int GameHardID = 22;
    81.     private const int GameEasyID = 24;
    82.  
    83.     private const int DojoResumeID = 20;
    84.     private const int DojoNewID = 21;
    85.  
    86.     private const int BackID = 30;
    87.  
    88.     private int[] ChapterID ={20,21,22,23,24,25,26,27};
    89.  
    90.     private Vector3 Position;
    91.       private E_MenuState MenuState = E_MenuState.MainMenu;
    92.     private AudioSource Music;
    93.     const float MaxMusicVolume = 0.7f;
    94.  
    95.     void Awake()
    96.     {
    97.         Music = GetComponent<AudioSource>();
    98.         Position = transform.position;
    99.         Loading.SetActiveRecursively(false);
    100.     }
    101.        
    102.     void Start ()
    103.     {
    104.         Resources.UnloadUnusedAssets();
    105.  
    106.         StartCoroutine(FadeInMusic(0.1f));
    107.         Game.Instance.GameState = E_GameState.MainMenu;
    108.  
    109.        /* if (iPhoneUtils.isApplicationGenuine == false && iPhoneUtils.isApplicationGenuineAvailable == true)
    110.         {
    111.             int numberofTest = PlayerPrefs.GetInt("failed", 0);
    112.             numberofTest++;
    113.             Game.Instance.DisabledState = numberofTest;
    114.             PlayerPrefs.SetInt("failed", numberofTest);
    115.             //Camera.mainCamera.orthographicSize = 0.2f;
    116.         }
    117.         else*/
    118.             PlayerPrefs.SetInt("failed", 0);
    119.            
    120.  
    121.       //  StartCoroutine(StartNewEasyGame());
    122.      
    123.         if(openFeint == null){
    124.             // 创建脚本接口对象
    125. //            openFeint = new OpenFeintFacade();
    126.             // è®¾ç½®æ¸ é“æ ‡è¯†ï¼ˆä¸€å®šè¦åœ¨åˆå§‹åŒ–ä¹‹å‰è®¾ç½®ï¼Œå¦‚æžœä¸éœ€è¦æ¸ é“æ ‡è¯†ï¼Œå¯ä»¥ä¸ç”¨è®¾ç½®ï¼Œå…¶é»˜è®¤å€¼ä¸º"default")
    127.             //openFeint.setAppstore(The9Settings.appstoreName);
    128. //            openFeint.setAppstore("htctegra");
    129.             // 初始化九城游戏ä¸å¿ƒ
    130.         //    openFeint.Init(ofname, ofkey, ofsecret, ofid);
    131.         }
    132.     //    Debug.Log("hackyhack,samurai ||");
    133.     }
    134.  
    135.     public void Update()
    136.     {
    137.        
    138.         if(Input.touchCount == 0)
    139.             return;
    140.  
    141.         Touch t = Input.touches[0];
    142.  
    143.         if(t.phase != TouchPhase.Ended)
    144.             return;
    145.  
    146.         RaycastHit hit;
    147.         if (Physics.Raycast(Camera.main.ScreenPointToRay(t.position), out hit, Mathf.Infinity) == false)
    148.             return;
    149.  
    150.         int id = hit.collider.gameObject.layer;
    151.  
    152.         AudioSource.PlayClipAtPoint(SoundsButton[Random.Range(0, SoundsButton.Length)], Position);
    153.        
    154.         if (MenuState == E_MenuState.MainMenu)
    155.         {
    156.             switch(id)
    157.             {
    158.                 case SinglePlayerID:
    159.                     SinglePlayer.GetComponent<Animation>().Play("MainButtonsPress");
    160.                     StartCoroutine(StartSinglePlayerSubMenu());
    161. //                admobdemo.Showinter();
    162.                     break;
    163.                 case DojoID:
    164.                     Dojo.GetComponent<Animation>().Play("MainButtonsPress");
    165.                     StartCoroutine(StartDojoSubMenu());
    166.                     break;
    167.                 case ChaptersID:
    168.                     Chapters.GetComponent<Animation>().Play("MainButtonsPress");
    169.                     StartCoroutine(StartChaptersSubMenu());
    170.                     break;
    171.                 case LeaderboardsID:
    172.                     Leaderboards.GetComponent<Animation>().Play("AandL_press");
    173.                     StartCoroutine(StartLeaderboards());
    174.                     break;
    175.                 case AchievementsID:
    176.                     Achievements.GetComponent<Animation>().Play("AandL_press");
    177.                     StartCoroutine(StartAchievements());
    178.                     break;
    179.                 case GameCenterID:
    180.                     //GameCenter.animation.Play("AandL_press");
    181.                 Application.OpenURL("");
    182.                     break;
    183.                 case HelpID:
    184.                     Help.GetComponent<Animation>().Play("MainButtonsPress");
    185.                     StartCoroutine(StartTutorial());
    186.             //    Advertisement.Show();
    187.                     break;
    188.                 case MoreGamesID:
    189.               //      MoreGames.GetComponent<Animation>().Play("MainButtonsPress");
    190.                 //Application.OpenURL("https://play.google.com/store/apps/dev?id=8072288438760313224");
    191.                     break;
    192.             }
    193.             //if(id != GameCenterID) {
    194.             //    GameCenter.animation.Play("MainButtonsOut");
    195.             //}
    196.         }
    197.         else if(MenuState == E_MenuState.DojoSubmenu)
    198.         {
    199.             switch(id)
    200.             {
    201.                 case DojoResumeID:
    202.                     DojoResume.GetComponent<Animation>().Play("press");
    203.                     StartCoroutine(StartResumeDojo());
    204.                     break;
    205.                 case DojoID:
    206.                     DojoNew.GetComponent<Animation>().Play("press");
    207.                     StartCoroutine(StartNewDojo());
    208.                     break;
    209.                 case BackID:
    210.                     Back.GetComponent<Animation>().Play("press");
    211.                     StartCoroutine(StartFromDojo2MainMenu());
    212.                     break;
    213.             }
    214.         }
    215.         else if (MenuState == E_MenuState.GameSubmenu)
    216.         {
    217.             switch(id)
    218.             {
    219.                 case GameResumeID:
    220.                     GameResume.GetComponent<Animation>().Play("press");
    221.                     StartCoroutine(StartResumeGame());
    222.                     break;
    223.                 case GameEasyID:
    224.                     GameEasy.GetComponent<Animation>().Play("press");
    225.                     StartCoroutine(StartNewEasyGame());
    226.                     break;
    227.                 case GameNormalID:
    228.                     GameNormal.GetComponent<Animation>().Play("press");
    229.                     StartCoroutine(StartNewNormalGame());
    230.                     break;
    231.                 case GameHardID:
    232.                     GameHard.GetComponent<Animation>().Play("press");
    233.                     StartCoroutine(StartNewHardGame());
    234.                     break;
    235.                 case BackID:
    236.                     Back.GetComponent<Animation>().Play("press");
    237.                     StartCoroutine(StartFromGame2MainMenu());
    238.                     break;
    239.             }
    240.         }
    241.         else if (MenuState == E_MenuState.ChaptersSubmenu)
    242.         {
    243.             if(id == BackID)
    244.             {
    245.                 Back.GetComponent<Animation>().Play("press");
    246.                 StartCoroutine(StartFromChapters2MainMenu());
    247.             }
    248.             else
    249.             {
    250.                 int  index =  id - ChapterID[0];
    251.             //    Debug.Log("Chapter Index"+index);
    252.             //    Debug.Log("Chapter ID = " + ChapterID[0]);
    253.             //    Debug.Log("id"+id);
    254.              Chapter[index].GetComponent<Animation>().Play("press");
    255.                 StartCoroutine(StartChapter(index));
    256.             }
    257.         }
    258.     }
    259.  
    260.     void FadeIn()
    261.     {
    262.         MenuState = E_MenuState.MainMenu;
    263.     }
    264.  
    265. //    void GameCenterPlayerLog()
    266. //    {
    267. //        if (MenuState == E_MenuState.MainMenu)
    268. //            StartCoroutine(_StartGameCenterMenu());
    269. //    }
    270.  
    271. //    IEnumerator _StartGameCenterMenu()
    272. //    {
    273. //        MenuState = E_MenuState.Busy;
    274. //        ALBackground.GetComponent<Animation>().Play("AandL_out");
    275. //        GameCenter.GetComponent<Animation>().Play("AandL_out");
    276. //
    277. //      
    278. //
    279. //        yield return new WaitForSeconds(0.2f);
    280. //
    281. //        ALBackground.GetComponent<Animation>().Play("AandL_in");
    282. //        Achievements.GetComponent<Animation>().Play("AandL_in");
    283. //        Leaderboards.GetComponent<Animation>().Play("AandL_in");
    284. //
    285. //        MenuState = E_MenuState.MainMenu;
    286. //    }
    287.  
    288.     void GameCenterNotSupported()
    289.     {
    290.         if (MenuState == E_MenuState.MainMenu)
    291.         {
    292.             ALBackground.GetComponent<Animation>().Play("AandL_out");
    293.             GameCenter.GetComponent<Animation>().Play("AandL_out");
    294.         }
    295.     }
    296.  
    297.     void GameCenterPlayerFailedToLog(string error)
    298.     {
    299.         if (MenuState == E_MenuState.MainMenu)
    300.             StartCoroutine(_ShowGameCenter());
    301.     }
    302.  
    303.     void GameCenterPlayerLogOut()
    304.     {
    305.         if (MenuState == E_MenuState.MainMenu)
    306.             StartCoroutine(_ShowGameCenter());
    307.     }
    308.  
    309.     IEnumerator _ShowGameCenter()
    310.     {
    311.         MenuState = E_MenuState.Busy;
    312.  
    313.         ALBackground.GetComponent<Animation>().Play("AandL_out");
    314.         Achievements.GetComponent<Animation>().Play("AandL_out");
    315.         Leaderboards.GetComponent<Animation>().Play("AandL_out");
    316.  
    317.         yield return new WaitForSeconds(0.2f);
    318.  
    319.         ALBackground.GetComponent<Animation>().Play("AandL_in");
    320.         GameCenter.GetComponent<Animation>().Play("AandL_in");
    321.  
    322.         MenuState = E_MenuState.MainMenu;
    323.     }
    324.  
    325.     IEnumerator StartSinglePlayerSubMenu()
    326.     {
    327.         MenuState = E_MenuState.Busy;
    328.         yield return new WaitForSeconds(0.1f);
    329.  
    330.         AudioSource.PlayClipAtPoint(SoundMainMenuOut, Position);
    331.  
    332.         MainBackground.GetComponent<Animation>().Play("toSubmenu");
    333.         MainLine.GetComponent<Animation>().Play("ButtonLineOut");
    334.         SinglePlayer.GetComponent<Animation>().Play("MainButtonsOut");
    335.         Dojo.GetComponent<Animation>().Play("MainButtonsOut");
    336.         Chapters.GetComponent<Animation>().Play("MainButtonsOut");
    337.         Help.GetComponent<Animation>().Play("MainButtonsOut");
    338.         MoreGames.GetComponent<Animation>().Play("MainButtonsOut");
    339.         GameCenter.GetComponent<Animation>().Play("MainButtonsOut");
    340.  
    341.         yield return new WaitForSeconds(0.3f);
    342.  
    343.         GameBackground.GetComponent<Animation>().Play("in");
    344.         GameTitle.GetComponent<Animation>().Play("TitleIn");
    345.  
    346.         if (Game.Instance.IsResumePossible(E_GameType.SinglePlayer))
    347.             GameResume.GetComponent<Animation>().Play("in");
    348.  
    349.         GameEasy.GetComponent<Animation>().Play("in");
    350.         GameNormal.GetComponent<Animation>().Play("in");
    351.         GameHard.GetComponent<Animation>().Play("in");
    352.         Back.GetComponent<Animation>().Play("in");
    353.  
    354.         AudioSource.PlayClipAtPoint(SoundSubMenuIn, Position);
    355.  
    356.         MenuState = E_MenuState.GameSubmenu;
    357.  
    358.     }
    359.  
    360.     IEnumerator StartDojoSubMenu()
    361.     {
    362.         MenuState = E_MenuState.Busy;
    363.         yield return new WaitForSeconds(0.1f);
    364.  
    365.         AudioSource.PlayClipAtPoint(SoundMainMenuOut, Position);
    366.        
    367.         MainBackground.GetComponent<Animation>().Play("toSubmenu");
    368.         MainLine.GetComponent<Animation>().Play("ButtonLineOut");
    369.         SinglePlayer.GetComponent<Animation>().Play("MainButtonsOut");
    370.         Dojo.GetComponent<Animation>().Play("MainButtonsOut");
    371.         Chapters.GetComponent<Animation>().Play("MainButtonsOut");
    372.         Help.GetComponent<Animation>().Play("MainButtonsOut");
    373.         MoreGames.GetComponent<Animation>().Play("MainButtonsOut");
    374.         GameCenter.GetComponent<Animation>().Play("MainButtonsOut");
    375.  
    376.         yield return new WaitForSeconds(0.3f);
    377.  
    378.         AudioSource.PlayClipAtPoint(SoundSubMenuIn, Position);
    379.  
    380.         DojoBackground.GetComponent<Animation>().Play("in");
    381.         DojoTitle.GetComponent<Animation>().Play("TitleIn");
    382.  
    383.         if (Game.Instance.IsResumePossible(E_GameType.Survival))
    384.             DojoResume.GetComponent<Animation>().Play("in");
    385.        
    386.         DojoNew.GetComponent<Animation>().Play("in");
    387.         Back.GetComponent<Animation>().Play("in");
    388.  
    389.         MenuState = E_MenuState.DojoSubmenu;
    390.     }
    391.  
    392.     IEnumerator StartChaptersSubMenu()
    393.     {
    394.         MenuState = E_MenuState.Busy;
    395.         yield return new WaitForSeconds(0.1f);
    396.  
    397.         AudioSource.PlayClipAtPoint(SoundMainMenuOut, Position);
    398.  
    399.         MainBackground.GetComponent<Animation>().Play("toSubmenu");
    400.         MainLine.GetComponent<Animation>().Play("ButtonLineOut");
    401.         SinglePlayer.GetComponent<Animation>().Play("MainButtonsOut");
    402.         Dojo.GetComponent<Animation>().Play("MainButtonsOut");
    403.         Chapters.GetComponent<Animation>().Play("MainButtonsOut");
    404.         Help.GetComponent<Animation>().Play("MainButtonsOut");
    405.         MoreGames.GetComponent<Animation>().Play("MainButtonsOut");
    406.         GameCenter.GetComponent<Animation>().Play("MainButtonsOut");
    407.  
    408.         yield return new WaitForSeconds(0.3f);
    409.  
    410.         AudioSource.PlayClipAtPoint(SoundSubMenuIn, Position);
    411.  
    412.         ChaptersBackground.GetComponent<Animation>().Play("in");
    413.         ChaptersTitle.GetComponent<Animation>().Play("TitleIn");
    414.  
    415.         ChaptersLocked.GetComponent<Animation>().Play("in");
    416.  
    417.         int unlockedMissions = Game.Instance.GetUnlockedMission();
    418.         for (int i = 0; i < Chapter.Length && i < unlockedMissions; i++)
    419.         {
    420.             Chapter[i].GetComponent<Animation>().Play("in");
    421.         }
    422.  
    423.         Back.GetComponent<Animation>().Play("in");
    424.  
    425.         MenuState = E_MenuState.ChaptersSubmenu;
    426.  
    427.     }
    428.  
    429.     IEnumerator StartLeaderboards()
    430.     {
    431.         MenuState = E_MenuState.Busy;
    432.         yield return new WaitForSeconds(0.2f);
    433.  
    434.         yield return new WaitForSeconds(0.2f);
    435.  
    436.         MenuState = E_MenuState.MainMenu;
    437.     }
    438.  
    439.     IEnumerator StartAchievements()
    440.     {
    441.         MenuState = E_MenuState.Busy;
    442.         yield return new WaitForSeconds(0.2f);
    443.  
    444.         yield return new WaitForSeconds(0.2f);
    445.  
    446.         MenuState = E_MenuState.MainMenu;
    447.  
    448.     }
    449.  
    450. //    IEnumerator StartGameCenter()
    451. //    {
    452. //        MenuState = E_MenuState.Busy;
    453. //        yield return new WaitForSeconds(0.2f);
    454. //        openFeint.OpenDashboard();
    455. //        yield return new WaitForSeconds(0.2f);
    456. //
    457. //        MenuState = E_MenuState.MainMenu;
    458. //    }
    459.  
    460.     IEnumerator StartTutorial()
    461.     {
    462.         MenuState = E_MenuState.Busy;
    463.         yield return new WaitForSeconds(0.1f);
    464.         StartCoroutine(FadeOutMusic(4));
    465.         StartCoroutine(FadeInLoading());
    466.  
    467.         yield return new WaitForSeconds(0.3f);
    468.  
    469.         Game.Instance.StartTutorial();
    470.  
    471.         MenuState = E_MenuState.MainMenu;
    472.     }
    473.    
    474. //    IEnumerator StartMoreGamesSubMenu()
    475. //    {
    476. //        MenuState = E_MenuState.Busy;
    477. //        yield return new WaitForSeconds(0.1f);
    478. //        StartCoroutine(FadeOutMusic(4));
    479. ////        StartCoroutine(FadeInLoading());
    480. //
    481. //        yield return new WaitForSeconds(0.3f);
    482. //
    483. //        Application.Quit();
    484. //        /*
    485. //        Game.Instance.StartSaleScreens();
    486. //
    487. //        MenuState = E_MenuState.MainMenu;*/
    488. //    }
    489.  
    490.     IEnumerator StartResumeGame()
    491.     {
    492.            //MenuState = E_MenuState.Busy;
    493.         yield return new WaitForSeconds(0.1f);
    494.         StartCoroutine(FadeOutMusic(4));
    495.         StartCoroutine(FadeInLoading());
    496.         yield return new WaitForSeconds(0.3f);
    497.  
    498.         Game.Instance.ResumeSinglePlayer();
    499.     //    Advertisement.Show();
    500.     }
    501.  
    502.     IEnumerator StartNewEasyGame()
    503.     {
    504.         MenuState = E_MenuState.Busy;
    505.         yield return new WaitForSeconds(0.1f);
    506.  
    507.         StartCoroutine(FadeOutMusic(4));
    508.         StartCoroutine(FadeInLoading());
    509.         yield return new WaitForSeconds(0.3f);
    510.         Game.Instance.StartNewGame(E_GameDifficulty.Easy);
    511.     }
    512.  
    513.     IEnumerator StartNewNormalGame()
    514.     {
    515.            MenuState = E_MenuState.Busy;
    516.         yield return new WaitForSeconds(0.1f);
    517.  
    518.         StartCoroutine(FadeOutMusic(4));
    519.         StartCoroutine(FadeInLoading());
    520.         yield return new WaitForSeconds(0.3f);
    521.         Game.Instance.StartNewGame(E_GameDifficulty.Normal);
    522.     }
    523.     IEnumerator StartNewHardGame()
    524.     {
    525.         MenuState = E_MenuState.Busy;
    526.         yield return new WaitForSeconds(0.1f);
    527.  
    528.         StartCoroutine(FadeOutMusic(4));
    529.         StartCoroutine(FadeInLoading());
    530.  
    531.         yield return new WaitForSeconds(0.3f);
    532.         Game.Instance.StartNewGame(E_GameDifficulty.Hard);
    533.  
    534.     }
    535.     IEnumerator StartFromGame2MainMenu()
    536.     {
    537.            MenuState = E_MenuState.Busy;
    538.         yield return new WaitForSeconds(0.1f);
    539.  
    540.         AudioSource.PlayClipAtPoint(SoundSubMenuOut, Position);
    541.  
    542.         GameBackground.GetComponent<Animation>().Play("out");
    543.         GameTitle.GetComponent<Animation>().Play("TitleOut");
    544.        
    545.         if (Game.Instance.IsResumePossible(E_GameType.SinglePlayer))
    546.             GameResume.GetComponent<Animation>().Play("out");
    547.  
    548.         GameEasy.GetComponent<Animation>().Play("out");
    549.         GameNormal.GetComponent<Animation>().Play("out");
    550.         GameHard.GetComponent<Animation>().Play("out");
    551.         Back.GetComponent<Animation>().Play("out");
    552.  
    553.         yield return new WaitForSeconds(0.3f);
    554.  
    555.         AudioSource.PlayClipAtPoint(SoundMainMenuIn, Position);
    556.  
    557.         MainBackground.GetComponent<Animation>().Play("toMainMenu");
    558.         MainLine.GetComponent<Animation>().Play("ButtonLineIn");
    559.         SinglePlayer.GetComponent<Animation>().Play("MainButtonsIn");
    560.         Dojo.GetComponent<Animation>().Play("MainButtonsIn");
    561.         Chapters.GetComponent<Animation>().Play("MainButtonsIn");
    562.         Help.GetComponent<Animation>().Play("MainButtonsIn");
    563.         MoreGames.GetComponent<Animation>().Play("MainButtonsIn");
    564.         GameCenter.GetComponent<Animation>().Play("MainButtonsIn");
    565.  
    566.         MenuState = E_MenuState.MainMenu;
    567.     }
    568.  
    569.     IEnumerator StartResumeDojo()
    570.     {
    571.         MenuState = E_MenuState.Busy;
    572.         yield return new WaitForSeconds(0.1f);
    573.         StartCoroutine(FadeInLoading());
    574.         yield return new WaitForSeconds(0.3f);
    575.         Game.Instance.ResumeSurvivalMode();
    576.     }
    577.  
    578.     IEnumerator StartNewDojo()
    579.     {
    580.         MenuState = E_MenuState.Busy;
    581.         yield return new WaitForSeconds(0.1f);
    582.         StartCoroutine(FadeInLoading());
    583.         yield return new WaitForSeconds(0.3f);
    584.         Game.Instance.StartSurvivalMode();
    585.     }
    586.  
    587.     IEnumerator StartFromDojo2MainMenu()
    588.     {
    589.         MenuState = E_MenuState.Busy;
    590.         yield return new WaitForSeconds(0.1f);
    591.  
    592.         AudioSource.PlayClipAtPoint(SoundSubMenuOut, Position);
    593.  
    594.         DojoBackground.GetComponent<Animation>().Play("out");
    595.         DojoTitle.GetComponent<Animation>().Play("TitleOut");
    596.  
    597.         if (Game.Instance.IsResumePossible(E_GameType.Survival))
    598.             DojoResume.GetComponent<Animation>().Play("out");
    599.         DojoNew.GetComponent<Animation>().Play("out");
    600.         Back.GetComponent<Animation>().Play("out");
    601.  
    602.         yield return new WaitForSeconds(0.3f);
    603.  
    604.         AudioSource.PlayClipAtPoint(SoundMainMenuIn, Position);
    605.  
    606.         MainBackground.GetComponent<Animation>().Play("toMainMenu");
    607.         MainLine.GetComponent<Animation>().Play("ButtonLineIn");
    608.         SinglePlayer.GetComponent<Animation>().Play("MainButtonsIn");
    609.         Dojo.GetComponent<Animation>().Play("MainButtonsIn");
    610.         Chapters.GetComponent<Animation>().Play("MainButtonsIn");
    611.         Help.GetComponent<Animation>().Play("MainButtonsIn");
    612.         MoreGames.GetComponent<Animation>().Play("MainButtonsIn");
    613.         GameCenter.GetComponent<Animation>().Play("MainButtonsIn");
    614.  
    615.         MenuState = E_MenuState.MainMenu;
    616.     }                          
    617.  
    618.     IEnumerator StartChapter(int index)
    619.     {
    620.         MenuState = E_MenuState.Busy;
    621.         yield return new WaitForSeconds(0.1f);
    622.  
    623.         FadeOutMusic(4);
    624.         StartCoroutine(FadeInLoading());
    625.         yield return new WaitForSeconds(0.3f);
    626.         Game.Instance.StartChapterMode(index);
    627.         Debug.Log("testchapter");
    628.     }
    629.  
    630.     IEnumerator StartFromChapters2MainMenu()
    631.     {
    632.            MenuState = E_MenuState.Busy;
    633.         yield return new WaitForSeconds(0.1f);
    634.  
    635.         AudioSource.PlayClipAtPoint(SoundSubMenuOut, Position);
    636.  
    637.         ChaptersLocked.GetComponent<Animation>().Play("out");
    638.         ChaptersBackground.GetComponent<Animation>().Play("out");
    639.         ChaptersTitle.GetComponent<Animation>().Play("TitleOut");
    640.  
    641.         int unlockedMissions = Game.Instance.GetUnlockedMission();
    642.  
    643.         for (int i = 0; i < Chapter.Length && i < unlockedMissions; i++)
    644.             Chapter[i].GetComponent<Animation>().Play("out");
    645.  
    646.         Back.GetComponent<Animation>().Play("out");
    647.  
    648.         yield return new WaitForSeconds(0.1f);
    649.  
    650.         AudioSource.PlayClipAtPoint(SoundMainMenuIn, Position);
    651.  
    652.         MainBackground.GetComponent<Animation>().Play("toMainMenu");
    653.         MainLine.GetComponent<Animation>().Play("ButtonLineIn");
    654.         SinglePlayer.GetComponent<Animation>().Play("MainButtonsIn");
    655.         Dojo.GetComponent<Animation>().Play("MainButtonsIn");
    656.         Chapters.GetComponent<Animation>().Play("MainButtonsIn");
    657.         Help.GetComponent<Animation>().Play("MainButtonsIn");
    658.         MoreGames.GetComponent<Animation>().Play("MainButtonsIn");
    659.         GameCenter.GetComponent<Animation>().Play("MainButtonsIn");
    660.  
    661.         MenuState = E_MenuState.MainMenu;
    662.  
    663.     }
    664.    
    665.     IEnumerator FadeInMusic(float speed)
    666.     {
    667.         float volume = 0;
    668.         Music.Play();
    669.         while (volume < MaxMusicVolume)
    670.         {
    671.             volume += speed * Time.deltaTime * MaxMusicVolume;
    672.             if (volume > MaxMusicVolume)
    673.                 volume = MaxMusicVolume;
    674.  
    675.             //Debug.Log(volume);
    676.             Music.volume = volume;
    677.             yield return new WaitForEndOfFrame();
    678.         }
    679.     }
    680.  
    681.     IEnumerator FadeOutMusic(float speed)
    682.     {
    683.         float volume = MaxMusicVolume;
    684.         while (volume > 0)
    685.         {
    686.             volume -= speed * Time.deltaTime * MaxMusicVolume;
    687.             if (volume < 0)
    688.             {
    689.                 Music.Stop();
    690.                 volume = 0;
    691.             }
    692.  
    693.             Music.volume = volume;
    694.  
    695.             yield return new WaitForEndOfFrame();
    696.         }
    697.     }
    698.  
    699.      IEnumerator FadeInLoading()
    700.     {
    701.         Loading.SetActiveRecursively(true);
    702.         Material mat = Loading.GetComponent<MeshRenderer>().material;
    703.  
    704.         Color color = new Color(1, 1, 1, 0);
    705.         mat.SetColor("_Color", color);
    706.  
    707.         while (color.a < 1)
    708.         {
    709.             color.a += Time.deltaTime * 10;
    710.             if (color.a > 1)
    711.                 color.a = 1;
    712.  
    713.             mat.SetColor("_Color", color);
    714.             yield return new WaitForEndOfFrame();
    715.         }
    716.  
    717.         color.a = 1;
    718.         mat.SetColor("_Color", color);
    719.     }
    720.  
    721.      void OnApplicationFocus(bool focus)
    722.      {
    723.          if(focus && Music)
    724.              StartCoroutine(FadeInMusic(0.1f));
    725.      }
    726.  
    727.  
    728. }
    729.  
     
  13. shahroziub

    shahroziub

    Joined:
    Sep 8, 2019
    Posts:
    21
    issue is on line 250
     
  14. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Oh good, you found the problem!
     
  15. shahroziub

    shahroziub

    Joined:
    Sep 8, 2019
    Posts:
    21
    yeah Found Problem but now solution :(
     
  16. shahroziub

    shahroziub

    Joined:
    Sep 8, 2019
    Posts:
    21
    i want to access index 7 "Chapter id 27 " but i can't able to do it shows me -10 index with exception
     
  17. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Why is it -10? Please see my previous post. Step through your logic to find the problem. Get a pencil and paper and make a grid like a spreadsheet. Along the top, write down the variable names. Down the left, write down the loop counter 1, 2, etc. Then go through your logic and start filling in the blanks.
     
    shahroziub likes this.
  18. shahroziub

    shahroziub

    Joined:
    Sep 8, 2019
    Posts:
    21