Search Unity

Application.LoadLevel(Application.loadedLevel+1) does not work

Discussion in 'Scripting' started by metin, Mar 27, 2015.

  1. metin

    metin

    Joined:
    Mar 14, 2014
    Posts:
    3
    Hello,

    I am using the 4.6 versiyon of Unity. I've been making a car parking game for Android. First of all, i created a level select scene. There are 5 level images ( first is open, others are locked ) on this scene. Then i created the first level. For the second level, i duplicated ( ctrl +d ) the first level. I changed only the two barrier's positions. When i try the game on the phone, i can park the car on the first level. Then i click to the nextlevel button. The second level appears with the all guitextures,car etc. Everything's done but the car does not move on the second level. How can i solve this problem?

    Here's the code part:

    if(replay.HitTest(t.position))
    {
    Destroy(this);
    Application.LoadLevel(Application.loadedLevel);

    }
    if(nextLevel.HitTest(t.position))
    {
    Destroy(this);
    Application.LoadLevel(Application.loadedLevel + 1);

    }

    By the way, the replay button works perfectly.
     
    Last edited: Mar 27, 2015
  2. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    Don't know what to tell you but here my LoadLevel works fine without destroying anything.

    In http://docs.unity3d.com/ScriptReference/Application.LoadLevel.html ... you'll read that "When loading a new level all game objects that have been loaded before are destroyed. If you want to let an object survive when loading a new level, use Object.DontDestroyOnLoad.". Maybe you don't need to Destroy(this) ..
     
  3. metin

    metin

    Joined:
    Mar 14, 2014
    Posts:
    3
    I deleted the "Destroy(this).

    It worked. Thank you so much!
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Destroy can do some odd things. Its not a bad idea to make Destroy the last line you expect to execute in any given script. Its not strictly needed, but it can avoid this sort of thing.

    Or it might not, I really should run some tests on it.