Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Another Loadlevel Query

Discussion in 'Scripting' started by AaronC, May 7, 2007.

  1. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Heya
    if
    Code (csharp):
    1. Application.LoadLevel (Random.Range(0, Application.levelCount-1));
    Loads a random level, How would I choose a random level specifically between Levels 3 and 10 (ignoring 1 and 2?
    Thanks
    AC
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1.  
    2. Application.LoadLevel (Random.Range(3, 11));
    3.  
    If you use integers, the random range function returns a number from the first to (but not including) the second. If you use floats, the function returns a number from the first to (and including) the second.

    So I believe that code for a random level should be

    Code (csharp):
    1.  
    2. Application.LoadLevel (Random.Range(0, Application.levelCount));
    3.  
    Or else there's no possibility of ever getting the last level.

    --Eric
     
  3. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Your so friggin helpful Eric 8) !

    Seriously, if theres anything I can help you with art wise, let me know...I hope to have a fully licensed version of Maya within the next month...

    That Space invaders clone perhaps?

    PM me if theres anything I can do for you.
    AaronC
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    At the moment I do all my own art, programming, sound, etc. If I ever learn to stop being such a control freak (it does make projects take a long time to finish), I'll let you know. :)

    Hang on--I've never done any character animation and I doubt I'd be very good at creating humans. So there's that, if I ever do anything that needs people.

    --Eric
     
  5. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    @Eric-Im working on a few characters this year, so when ive got something worth showing I'll let you know...

    Now About levelWasLoaded:

    My GO is an empty with an audio clip attatched. It also has a script marked dont destroy on load, and currently Im checking to see if a variety of levels were loaded in the Update function. I think this may be uneconomic. In summary, are awake or Start (re)called when a new level is loaded? If I could use either at the initialisation of a new level, that would seem to be more efficent than update.

    Thanks y'all
    AC
     
  6. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Instead of using an Update function (which gets called every update needlessly) why not use OnLevelWasLoaded instead? It gives you the integer index of the level that was just loaded and seems ripe for use in your example.

    Edit: and to be clear, Awake() and Start() will each only be called once during the lifetime of the script.
     
  7. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Great Tom! Thanks
    AC