- Home /
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
}
}
}
}
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.
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.
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.
@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.
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 calledGameOver scene is loaded
If you comment out the LoadLevel
part in OnDestroy
it does load the Win scene properly, doesn't it ?
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.
@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.
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");
}
}