Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by pinkanna · Jun 25, 2014 at 02:03 AM · c#2dbuildscenelevel

Not loading correct Scene/Level

I'm literally...99% done with making my first little game. The only thing standing in my way is ending it off.

I'm having an issue loading my "Win" screen after the player kills all enemies. It seems to load my Game Over scene instead. If my player dies, my game over scene loads, if my player wins, my game over scene loads. It never calls my Win scene. All other scenes load fine.

I have all my scenes in the build setting as follows.

  • 0-Intro

  • 1-Menu

  • 2-Instructions

  • 3-Scene1(main game)

  • 4.Win

  • 5.Gameover

Below is my GameWin Script, that I have placed in the scripts game object on my main scene..it's supposed to call the "Win" scene. I know it works because, if I change "Win" to a scene that doesn't exist, it gives me an error stating that the scene is not in the build settings.

Example...

Level 'Winner' (-1) couldn't be loaded because it has not been added to the build settings. To add a level to the build settings use the menu File->Build Settings... UnityEngine.Application:LoadLevel(String) GameWinScript:Update() (at Assets/Scripts/GameWinScript.cs:11)

It's just not bringing up the right scene. I've tried moving the scene down and up in the build settings with the same results...

 using UnityEngine;
 using System.Collections;
 
 public class GameWinScript : MonoBehaviour {
 
 
     
     void Update()
     {
         if ((GameObject.FindGameObjectsWithTag("Enemy")).Length == 0)
             Application.LoadLevel("Win");
     }
 
     }

Please help guys, any idea on what I could be doing wrong? This is the last piece...

EDIT to show more scripts...

Player Script (OnDestroy)

 using UnityEngine;
 
 /// <summary>
 /// Player controller and behavior
 /// </summary>
 public class PlayerScript : MonoBehaviour
 {
     /// <summary>
     /// 1 - The speed of the ship
     /// </summary>
     public Vector2 speed = new Vector2(50, 50);
     
     // 2 - Store the movement
     private Vector2 movement;
     
     void Update()
     {
         // 3 - Retrieve axis information
         float inputX = Input.GetAxis("Horizontal");
         float inputY = Input.GetAxis("Vertical");
         
         // 4 - Movement per direction
         movement = new Vector2(
             speed.x * inputX,
             speed.y * inputY);
 
         // ...
         
         // 5 - Shooting
         bool shoot = Input.GetButtonDown("Fire1");
         shoot |= Input.GetButtonDown("Fire2");
         // Careful: For Mac users, ctrl + arrow is a bad idea
         
         if (shoot)
         {
             WeaponScript weapon = GetComponent<WeaponScript>();
             if (weapon != null)
             {
                 // false because the player is not an enemy
                 weapon.Attack(false);
                 SoundEffectsHelper.Instance.MakePlayerShotSound();
             }
         }
         
         // ...
         
         // 6 - Make sure we are not outside the camera bounds
         var dist = (transform.position - Camera.main.transform.position).z;
         
         var leftBorder = Camera.main.ViewportToWorldPoint(
             new Vector3(0, 0, dist)
             ).x;
         
         var rightBorder = Camera.main.ViewportToWorldPoint(
             new Vector3(1, 0, dist)
             ).x;
         
         var topBorder = Camera.main.ViewportToWorldPoint(
             new Vector3(0, 0, dist)
             ).y;
         
         var bottomBorder = Camera.main.ViewportToWorldPoint(
             new Vector3(0, 1, dist)
             ).y;
         
         transform.position = new Vector3(
             Mathf.Clamp(transform.position.x, leftBorder, rightBorder),
             Mathf.Clamp(transform.position.y, topBorder, bottomBorder),
             transform.position.z
             );
 
     }
 
     //PlayerScript.cs
     //....
 
 
     void OnCollisionEnter2D(Collision2D collision)
     {
         bool damagePlayer = false;
         
         // Collision with enemy
         EnemyScript enemy = collision.gameObject.GetComponent<EnemyScript>();
         if (enemy != null)
         {
             // Kill the enemy
             HealthScript enemyHealth = enemy.GetComponent<HealthScript>();
             if (enemyHealth != null) enemyHealth.Damage(enemyHealth.hp);
             
             damagePlayer = true;
         }
         
         // Damage the player
         if (damagePlayer)
         {
             HealthScript playerHealth = this.GetComponent<HealthScript>();
             if (playerHealth != null) playerHealth.Damage(1);
         }
     }
     
     void FixedUpdate()
     {
         // 5 - Move the game object
         rigidbody2D.velocity = movement;
     }
 
     void OnDestroy()
     {
         // Game Over.
         // Add the script to the parent because the current game
         // object is likely going to be destroyed immediately.
         Application.LoadLevel("GameOver");
     }
 }


Game Over Script

 /// Start or quit the game
 /// </summary>
 public class GameOverScript : MonoBehaviour
 {
     void OnGUI()
     {
         const int buttonWidth = 120;
         const int buttonHeight = 60;
         
         if (
             GUI.Button(
             // Center in X, 1/3 of the height in Y
             new Rect(
             Screen.width * .3f, Screen.height * .74f,
             buttonWidth,
             buttonHeight
             ),
             "Retry!"
             )
             )
         {
             // Reload the level
             Application.LoadLevel("Scene1");
         }
         
         if (
             GUI.Button(
             // Center in X, 2/3 of the height in Y
             new Rect(
             Screen.width * .62f, Screen.height * .74f,
             buttonWidth,
             buttonHeight
             ),
             "Back to menu"
             )
             )
         {
             // Reload the level
             Application.LoadLevel("Menu");
         }
     }
 }


Edit #2 to include health script

Health Script

 using UnityEngine;
 
 /// <summary>
 /// Handle hitpoints and damages
 /// </summary>
 public class HealthScript : MonoBehaviour
 {
     /// <summary>
     /// Total hitpoints
     /// </summary>
     public int hp = 1;
     
     /// <summary>
     /// Enemy or player?
     /// </summary>
     public bool isEnemy = true;
     
     /// <summary>
     /// Inflicts damage and check if the object should be destroyed
     /// </summary>
     /// <param name="damageCount"></param>
     public void Damage(int damageCount)
     {
         hp -= damageCount;
         
         if (hp <= 0)
         {
             // Dead!
             Destroy(gameObject);
         }
     }
     
     void OnTriggerEnter2D(Collider2D otherCollider)
     {
         // Is this a shot?
         ShotScript shot = otherCollider.gameObject.GetComponent<ShotScript>();
         if (shot != null )
         
         {
             Debug.Log("Player Triggered by " + otherCollider.gameObject.name + ", Shot is: " + 
                       shot.isEnemyShot.ToString() + ", I am: " + isEnemy.ToString() + ", shot damage: " +
                       shot.damage.ToString() + ", health: " + hp.ToString() + ", Time: " +
                       Time.timeSinceLevelLoad.ToString());
 
             // Avoid friendly fire
             if (shot.isEnemyShot != isEnemy)
             {
                 Damage(shot.damage);
                 
                 // Destroy the shot
                 Destroy(shot.gameObject); // Remember to always target the game object, otherwise you will just remove the script
 
         
             }
     
         }
     
     }
 }





Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jun 25, 2014 at 02:13 AM 0
Share

Since your game switches to the GameOverScreen ins$$anonymous$$d, the problem might be there. How do you detect if your player died? Also where are those scripts attached? Are they destroyed when you load the win / gameover levels or are they still there (DontDestroyOnLoad?)

I guess that your gameover script just checks if the player is still there and if not it loads the gameover level. If the scripts are on a persistant object the gameover condition will be true as soon as you load the win level since your player will be removed.

However, just a guess since you don't know anything about your scene and gameover script.

avatar image Pyrian · Jun 25, 2014 at 02:45 AM 0
Share

I second Bunny83's guess. You call LoadLevel("Win"), which Destroys all your GameObjects first, and in that process, your Player GameObject is destroyed. At that point, you call LoadLevel("Gameover"), which now takes precedence.

This explains all your symptoms - if it's true.

avatar image Bunny83 · Jun 25, 2014 at 02:50 AM 0
Share

But that would only happen when the GameOver script is persistant. If however the script is on the player itself and uses OnDestroy / OnDisable it would be obvious ;)

Let's just wait for a reply.

avatar image pinkanna · Jun 25, 2014 at 03:00 AM 0
Share

@meat5000 Thanks for responding. I changed to 4 and I still have the same result...and no I didn't save over my scene, I have custom game win image and I see it fine under my scenes folder.

@Bunny83 Thank you for responding

How I detect if the player dies On the player, under void OnDestroy, I load the game over level/scene... I have I will update my post with more code for you to see

About the GameOverScript The game over script just shows the gui buttons to either reload the game or go to the menu. This is placed in a script game object on the GameOverScreen/Level. I will update my post to show you if that helps..

About placement of the gameover and gamewin script As stated earlier, the gameoverscript is nothing but buttons that load the scene1 scene (main game) and main menu scene. It's placed on the the GameOver scene, which It is called by void OnDestroy on the player script...

I placed the game win script in a game object called script on Scene 1 (main game), it's supposed to call the "Win" scene.

As of right now, the win scene just has a custom game win background and I planned to add a script to show buttons to start the game over...but haven't done so yet.

I hope this answered some of your questions, I will update my post now.

avatar image Maerig · Jun 25, 2014 at 03:23 AM 0
Share

If the GameOver scene is loaded as soon as the player is destroyed , then this could be what happens :

  • Player wins

  • Win scene is loaded

  • Current scene is destroyed

  • Player is destroyed

  • Player's OnDestroy method is called

  • GameOver scene is loaded

If you comment out the LoadLevel part in OnDestroy it does load the Win scene properly, doesn't it ?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Jun 25, 2014 at 03:19 AM

Well, like we kind of guessed you use OnDestroy to load the "GameOver" level. The problem with OnDestroy is, you can't stop it from calling when the object gets destroyed. It's actually not a good way to detect if the player died since there are other reasons why the object got destroyed.

It's better to do the gameover check inside the Damage(1) function of your player health script. That should solve your problem.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pinkanna · Jun 25, 2014 at 03:49 AM 0
Share

@Bunny83 sorry did a blockquote...;)

     if (damagePlayer)
         {
             HealthScript playerHealth = this.GetComponent<HealthScript>();
             if (playerHealth != null) playerHealth.Damage(1);
             Application.LoadLevel("GameOver");
         }

Added to player script but only works when the player collides with an enemy, not if the enemy shoots my player.

I have a health script on my player and I believe that needs to be manipulated so it works fully? Do you guys have any suggestions on where to put the load level?

Note: Edited post to show health script. And excuse me if I sound dense, but I'm very new to this and following a tutorial.

avatar image Pyrian · Jun 25, 2014 at 03:56 AM 1
Share

Just put it in the Damage function right after (or even ins$$anonymous$$d of) "Destroy(gameObject);". Since you use the same health script for players and enemies, you'll have to check which it is.

So:

 public void Damage(int damageCount)
 {
     hp -= damageCount;
 
     if (hp <= 0)
     {
         // Dead!
         if (isEnemy)
             Destroy(gameObject);
         else
             Application.LoadLevel("GameOver");
     }
 }


avatar image pinkanna · Jun 25, 2014 at 04:29 AM 0
Share

It works, thank you @Pyrian and @Bunny83

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

How to create a save & load system with: System.Serializable ??? 1 Answer

DontDestroyOnLoad cancelling when Main Menu is open 1 Answer

I got 60 errors while trying to make a build for my android game 0 Answers

Possible to load scenes from directory? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges