Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Scene Linking

Discussion in 'Scripting' started by SpiritWebb, Nov 25, 2009.

  1. SpiritWebb

    SpiritWebb

    Joined:
    Nov 10, 2009
    Posts:
    38
    Code (csharp):
    1.  
    2. var availableLevels : String[];var windowWidth : int = 200;
    3. var windowHeight : int = 200;
    4. private var showMenu : boolean = false;
    5.  
    6. function TriggerAction () {  //Replace TriggerAction with your desired means of triggering the menu.    showMenu = true;
    7. }
    8.  
    9. function OnGUI () {    if (showMenu) {        GUI.Window(0,Rect((Screen.width / 2) - (windowWidth / 2),  
    10. (Screen.height / 2) - (windowHeight / 2),                         windowWidth,                         windowHeight),                    LevelSelect,                    "Select a level!");  //Creates a window with ID 0 in the center of the screen using the function LevelSelect with the title, "Select a level!"    }
    11. }
    12.  
    13. function LevelSelect (id : int) {    
    14. for (var levelName : String in availableLevels) {        
    15. if (GUILayout.Button(levelName)) {            Application.LoadLevel(levelName);        
    16. }    
    17. }    
    18. if (GUILayout.Button("Cancel")) {  //Gives the player the opportunity to back out of the menu.        showMenu = false;    }
    19. }
    20.  
    So I got this code from someone else for multiple choice locations.

    But what I want to know, is can I have seperate locations, say 2 on one object. 3 on another location. Enter a new scene, that object has 1 or 2, another 3, etc.

    I am building a space project and want the player to be able to travel to other star systems via a phase gate. But each gate I want more then just one location...can this be done? If so, how can the above code be altered to get the effect?
     
  2. Alric

    Alric

    Joined:
    Feb 17, 2009
    Posts:
    331
    Absolutely, if I understand what you're asking right, then you don't need to change the code at all. Just pass in a different set of values in availableLevels, for each gate.
     
  3. SpiritWebb

    SpiritWebb

    Joined:
    Nov 10, 2009
    Posts:
    38
    Alright, will give that a try and see what happens tonight! I will reply with the results...
     
  4. SpiritWebb

    SpiritWebb

    Joined:
    Nov 10, 2009
    Posts:
    38
    So I tested the code, and I got:

    Level could not be loaded because its not added to the build settings. It is, I looked.

    So there has to be an error in the code or something, and I cannot find it...

    grrr...help please...
     
  5. SpiritWebb

    SpiritWebb

    Joined:
    Nov 10, 2009
    Posts:
    38
    Nevermind I figured out the problem and works quite nicely!
     
  6. SpiritWebb

    SpiritWebb

    Joined:
    Nov 10, 2009
    Posts:
    38
    Alright,

    So I ran into another bit of a problem with the linking.

    I know that when creating a new level (new star system in my case) you place the player down where they will arrive/start.

    Well going to the new star system works beautifully. My question is, when I return back through the gate, I start where the player first starts on the otherside of the system.

    How can I correct this to get the player to actually arrive at each gate?
     
  7. trepaning

    trepaning

    Joined:
    Nov 17, 2009
    Posts:
    8
    It would help future users immensely if you were to explain what you figured out, thanks.
     
  8. SpiritWebb

    SpiritWebb

    Joined:
    Nov 10, 2009
    Posts:
    38

    In the inspector's tab, once you add the script to the jumpgate, click the little arrow next to available levels. Set the number to how many you want to link to. It will show: 0, 1, 2, etc. In the blank spot off to the right of each number, you can click the spot, and type in the name of the level.

    It will now show up on the GUI once entering the jumpgate!! :)
     
  9. SpiritWebb

    SpiritWebb

    Joined:
    Nov 10, 2009
    Posts:
    38
    Anyone have ideas on this?
     
  10. SarperS

    SarperS

    Joined:
    Mar 10, 2009
    Posts:
    817
    Create an invisible "Spawn Point" prefab (You can assign a gizmo to an empty game object to achieve it)

    When the scene starts, transfer the player to the spawn point like for example "transform.position = spawnPoint.transform.position"

    As soon as you leave the scene, transfer the Spawn Point to wherever you like the player to appear when he arrives back to that star system.

    And voila, as it will use the script to transfer the player to the spawn point each time a scene starts, it's only a couple of lines of code to achieve the wanted effect. Just make sure you have a spawn point prefab in all the scenes, where the player will appear.

    This is what I've used in the past and it works like a charm. PM me if you need more help.
     
  11. SpiritWebb

    SpiritWebb

    Joined:
    Nov 10, 2009
    Posts:
    38
    Will this not affect the starting point, when first loading the game?
     
unityunity