Search Unity

How to load a new level when the game is finished?

Discussion in 'Scripting' started by Barft, Apr 27, 2015.

  1. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    (I ask this question on the unity answers page but it didn't work. )

    Hello! I am working on a game where you have 4 holes and 4 balls. In every hole must go in a ball, if that is done i want to load a new scene. But i really dont know how i make this. Can someone give my a script for this? I am searching now 2 months for this problem but i cant find a solution. (I write in C#)

    Btw: maybe something with a trigger system?
     
  2. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    I don't think anyone is going to just write you a script. I will point you in the right direction though. Set up some variables so you know when all of the holes have been filled. Once that has happened use Application.LoadLevel() to load your next level. Again, I could write the script for you, but it wouldn't teach you anything.

    Look into OnTriggerEnter and Application.LoadLevel; you also might want to look into using boolean variables if you are not familiar with them.
    Good luck! :)
     
  3. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    Hello, thanks for your reaction, I am new to programming in C# so i dont have really a lot of experience. I can make it with one ball and one hole but with 2 or 4 not. Thats why i ask for a script :), but what you said here above that thought i too but i dont know more than that...
     
  4. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Let me see what I can do about writing a small example script on how I would approach your problem. Give me half an hour.
     
  5. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    I haven't actually tested this, but if it doesn't work it is at least really close to something you can use. Attach this first script to your holes.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HoleTrigger : MonoBehaviour {
    5.  
    6.     public int index;
    7.  
    8.     public TriggerAndLoad triggerAndLoad {
    9.         get{
    10.             return GameObject.FindObjectOfType<TriggerAndLoad>();
    11.         }
    12.     }
    13.  
    14.     void OnTriggerEnter(Collider col) {
    15.         triggerAndLoad.TriggerHole(index);
    16.     }
    17.  
    18. }
    19.  
    Attach this script to any object in the scene that isn't going to be destroyed. I usually handle things like this with a "Game Master" object that just exists for the purpose of holding things like this.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TriggerAndLoad : MonoBehaviour {
    5.     public bool[] holeTrigger = new bool[4];
    6.  
    7.     public void TriggerHole(int index) {
    8.         holeTrigger[index] = true; //Set the hole trigger to true
    9.  
    10.         //Check if all holes have been triggered
    11.         if(AllTriggered)
    12.             Application.LoadLevel(0); //Modify this index to whichever level you want to load.
    13.     }
    14.  
    15.     //This method will iterate through each of the triggers in the array and return false if any of them are not true.
    16.     public bool AllTriggered {
    17.         get{
    18.             bool b = true;
    19.             foreach(bool bX in holeTrigger){
    20.                 if(!bX)
    21.                     b = false;
    22.             }
    23.             return b;
    24.         ;}
    25.     }
    26.    
    27. }
    28.  
    If you have any issues with implementation. Let me know. Best of luck to you!
     
  6. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    First, thanks for sending me a script:), the script has no erros, but i have attach the HoleTrigger script to al the 4 holes and they are trigger and the TriggerAndLoad to the 4 balls, and i added by application.loadlevel(); a new scene but it dousn't work :(
     
  7. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    No no no...you only want one TriggerAndLoad.cs in the scene. Don't put it on the balls haha. If you have more than one present in the scene you are gonna have issues.
     
  8. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Also, you need to set up the indices on your hole trigger scripts. One should have an index of 0, another should be 1, then 2, then 3. If you don't set it up like that it will not work. I guess I should have explained that as well. You can change the index via the public variable in the inspector.
     
  9. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    ow, but where must i added the script?
     
  10. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Make a new GameObject in the hierarchy and call it "Game Master" or something like that. Attach TriggerAndLoad to that object. Do not attach it to the balls. You only want one of them. Think of it like this...TriggerAndLoad.cs is a server and the 4 instances of the hole trigger script are clients that communicate with it. They all communicate with the same instance of the TriggerAndLoad script.
     
  11. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    I am a little confused, i have made a cube in the hierarchy and called it Game Master and attached it with the TriggerAndLoad script. But it still dont work
     
  12. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    I think i'm doing something wrong
     
  13. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    lol, I did tell you I didn't test the script. Let me test it from my end and see if I can get it working.
     
  14. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    I can't say if you are or not as I didn't test the script. I know there are no errors and I kind of just assumed it would work without ever testing it. Let me set up a quick scene and test it myself.
     
  15. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    Ow haha, I'm sorry, i am a 14 year old boy that lives in the netherlands so my english is not the best :)
     
  16. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    Btw i really really thank you, you will help me:)
     
  17. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    It's fine on my end @Barft . Here's a few things to consider:

    1. Balls need Rigidbodies
    2. Holes need triggers in them
    3. Indices of HoleTrigger.cs have to be set up at 0, 1, 2, and 3
    4. Make sure there is only one instance of TriggerAndLoad.cs in the scene. Make sure you removed it from your balls.

    Please see the attached unity package for proper usage
     

    Attached Files:

  18. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    I dont can view them, dont have permissions
     
  19. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    Ow, i got it
     
  20. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    That scene should just keep reloading over and over when you press play. That means it is working. I didn't change the code at all. I just set up the scene properly
     
  21. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    1. Balls nodig Rigidbodies
    2. Gaten moeten triggers in hen
    3. Indices van HoleTrigger.cs moeten worden ingesteld op 0, 1 , 2 , en 3
    4. Zorg ervoor dat er slechts één exemplaar van TriggerAndLoad.cs in de scène . Zorg ervoor dat u deze verwijderd uit je ballen

    Courtesy of Google Translate. I apologize if it's hard to read. I do not speak Dutch.
     
  22. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    What for an object is the game master?
     
  23. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    It's just an empty GameObject. No cube, no sphere, no mesh. Just an empty object with the script attached to it.
     
  24. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    IT WORKS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     
  25. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    I lOVE YOU, really man, i am searching for 2 months and i finally got it!!!!! I really really really want to thank you!!!:):)
     
    hamsterbytedev likes this.
  26. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    No worries. Just stick with it and you'll catch on. It's good to start young like you are. Just keep at it and try not to get discouraged. Programming isn't easy and it takes years to get comfortable with it. Best of luck in all of your future endeavours!
     
  27. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    @Barft, based on what I read, I'd recommend you watch C# and Unity tutorial videos. I'm not sure how well your English comprehension skills are, as you mentioned you're a 14 year old boy in the Netherlands, but it's at least worth watching the videos and downloading the sample coding and test out what everything does to understand what is happening.

    It helps to take the Unity scripts from the tutorials and change minor things in them to further understand what scripts do in Unity.

    Unity how to use the editor tutorials:
    http://unity3d.com/learn/tutorials/modules/beginner/editor

    Unity scripting tutorials:
    http://unity3d.com/learn/tutorials/modules/beginner/scripting

    Unity full game tutorial videos:
    http://unity3d.com/learn/tutorials/modules

    You can look for a local Unity meetup group and see if you can meet with people in the Netherlands:
    http://unity3d.com/community/user-groups
    https://www.facebook.com/DutchUnityUserGroup

    Finally, if you get stuck, the forums are a great resource for knowledge as you've already seen. Also, remember to never give up. You'll find the solution eventually. All the best. :)
     
  28. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    Thanks all:)
     
  29. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    Ow i got one problem. 3 of the 4 elements be active when the ball fall in the hole, but one does nothing. Do you know what the problem is?
     
  30. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    And it gives the error: Index is out of range
     
  31. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Are your indices set as 0, 1, 2, and 3? The length of the array is 4, but zero is included so the last index in the array is actually 3.
     
  32. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    o ok, i fixed it. Thanks!
     
  33. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    Hi guys, i got a new problem. But this time with GUI's. I have made a script and it looks like:

    float timeRemaining = 35;
    public Texture2D menuImage;
    public int width = 100;


    void OnGUI () {
    if (timeRemaining > 0) {
    GUI.DrawTexture(new Rect(Screen.width-100,20,70,28), menuImage);
    }
    }

    It set a image in de scene, it works but the image height is really low and the width is normal. What is the problem?
     
  34. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  35. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    Ok, thanks
     
  36. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    Hello , i didn't found the answer for my problem, can you please have a look to my new script:

    float timeRemaining = 35;
    public Texture2D menuImage;
    public int width = 100;


    void OnGUI () {
    if (timeRemaining > 0) {
    GUI.DrawTexture(new Rect(Screen.width-100,20,70,28, menuImage.width, menuImage.height), menuImage);
    }
    }

    I have one thing added, namely menuImage.width, and menuImage.height. Thats the solution for the problem. But unity gives some errors, and i don't understand what is wrong.