How do I switch scenes when player dies
I had made out an apply damage script . The problem I am having is the when the player health gets low the scene does not change . Here is my script :
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
[RequireComponent(typeof(Collider))]
public class ApplyDamage : MonoBehaviour {
//Drag this class on the object you want to modify player's health!
//This handles subtracting and adding health points to player in a convenient way.
/*As follows:
* Tick 'Is this healing player' true or false in the inspector (consequently it will add or subtract health points)
* Declare amount (on a 1000 scale not on 100!)
* Add a collider or trigger collider depending on your needs. See below the proper sections for different
* behaviors!
* */
public bool IsThisHealingPlayer;
public bool IsThisPickupable;
public int AmountOfHealth;
public bool instantDeath;
int health;
Healthbar healthbar;
void Start(){
healthbar = FindObjectOfType<Healthbar>();
if (healthbar == null) {
Debug.LogError("Healthbar class is not found in scene!");
}
if (IsThisHealingPlayer)
health = AmountOfHealth * -1;
else
health = AmountOfHealth * 1;
}
//Trigger collider:
//For zone damage (runs every frame and damages/heals player when inside trigger zone (eg. fire, radiation field, etc)
void OnTriggerStay(Collider other) {
if (other.transform.tag == "Player" && !IsThisPickupable) {
healthbar.SendMessage("ModifyHealth", health, SendMessageOptions.DontRequireReceiver);
}
}
//Trigger collider:
//For picking up medicine boxes or health potions
void OnTriggerEnter(Collider other){
if (other.transform.tag == "Player") {
if (instantDeath) {
healthbar.health = 0;
}
if (IsThisPickupable && healthbar.health < 1000) {
healthbar.health = healthbar.health - health;
healthbar.health = Mathf.Clamp(healthbar.health, 0, 1000);
Destroy(gameObject);
SceneManager.LoadScene("T");
}
}
}
//Plain collider (eg. sphere for bullet)
//This is to damage player on impact (rock falling, bullet hitting, etc)
//Note: Check collision matrix for ControllerColliderHit, also, colliding with character controller is a bit more
//complex so modify this section according to your needs.
void OnControllerColliderHit(ControllerColliderHit other) {
if (other.transform.tag == "Player") {
healthbar.SendMessage("ModifyHealth", health, SendMessageOptions.DontRequireReceiver);
}
}
/* This is to be used if the player is a rigidbody (eg. rollerball)
void OnCollisionEnter(Collision other) {
if (other.transform.tag == "Player") {
healthbar.SendMessage("ModifyHealth", health, SendMessageOptions.DontRequireReceiver);
}
}*/
}
Have you tried using debug.log to see if it's entering the OnTriggerEnter block?
Where IsThisPickupable set to true?
Answer by TBruce · Jun 29, 2016 at 02:00 AM
Hi importguru88,
I downloaded your project. I made a couple of obvious fixes
Currently everytime you run over a MedicalBox when your health is below 1000 it loads scene "T". I changed it to apply the amount of "Health points gained" as set in the inspector instead of loading a new scene.
Currently the "Sparkle Rising" area does nothing, I made a change/fix to apply the heal up to the player based on the "Heel up speed".
Now for some questions, and forgive me since I do not know where you are going with this:
First question, why are you destroying the gameObject in ApplyDamage.OnTriggerEnter() when you are loading a scene on the next line.
Second question, this is just me but by the current logic in ApplyDamage.OnTriggerEnter()
if (healthbar.health <= 0)
why are you loading a different scene. Why not the same scene.
Third question, and this applies to my second question above and your question
"The problem I am having is the when the player health gets low the scene does not change .
If lets say the player runs into the Fire or the Flame and the health falls to 0, what do you want to happen? Currently the health is restored, after a brief delay, and then the player starts afresh in the current level."
I want the scene to switch . I need to be able to move the health bar . When the health reaches zero , their going to the be a restart scene.
I want the scene to switch . I need to be able to move the health bar . When the health reaches zero , their going to the be a restart scene.
I did that on this if (healthbar.health <= 0) The scene do change .I got the scene to switch only this (healthbar.health > 0) . The thing is scene switches right when I enter the collider.
Here is the same project I only made some changes to the apply damage http://www.mediafire.com/download/7jgd6t6zyfux9yk/New+Unity+Project1.zip
I have it to where the player is taking damage from the cube when entering.
Take a look at this update to your project. Here are the updates made
$$anonymous$$ade it so that when you run over a $$anonymous$$edicalBox it would heal you the amount of "Health points gained" as set in the inspector if the players health is below 1000.
$$anonymous$$ade the "Sparkle Rising" area heal the player based on the "Heel up speed" if the players health is below 1000.
I made the scene T that is to be loaded a property which is set in the inspector in the HealthBar script. The name of the new property is called sceneToLoad.
Currently damage is applied in only the Fire and Flame areas. I made it so that anytime health is less than or equal to 0 sceneToLoad is loaded. This is implemented in the $$anonymous$$odifyHealth() function. (This also means that if you set up any other areas to harm the player, the scene will be loaded if the health drops to 0).
Also important, you must make sure that all scenes are set in the Build Settings window.
Please test this out and let me know if there are any questions or issues.
Answer by wesleywh · Jun 28, 2016 at 04:17 AM
Tip 1: The following line can be edited.
...
Destroy(gameObject);
SceneManager.LoadScene("T");
...
Switching scenes will destroy this object as well as long as "DontDestroyOnLoad" isn't applied to it. Just remove the destroy call.
Tip 2: Also if you are making this a multiplayer game doing something like this:
healthbar = FindObjectOfType<Healthbar>();
could potentially yield sketchy results if you plan to have more than one version of it in the scene at a time. From your script that seems to be indeed the case here. I would just explicity talk about the one your looking for
Example:
healthbar = this.GetComponent<HealthBar>();
So it could also be that if you have more than one of these in the scene it might be referencing the wrong one of it. Hope this helps.
Regarding Tip 1: A call to Destroy() doesn't destroy the object until the current update is finished. Also, the only way to stop execution from reaching the next line of code after the Destroy would be if Destroy throws an exception (which I don't think it does, even if the object to be destroyed is null).
I did some reading and you are correct in the it will finish the Update() call prior to the destruction of the object. I fixed my answer accordingly.
How should code it exactly ?I did this.component health bar. I gotten error saying its not in the scene .
Your answer
Follow this Question
Related Questions
Destroyed object in DontDestroyOnLoad does not recreate in its main scene 0 Answers
Cannot interact with new scene 0 Answers
using Scenemanager 1 Answer
Event when the scene is loaded 1 Answer