Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by importguru88 · Jun 27, 2016 at 10:10 PM · playerscenescene-loadingapply

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);
         }
     }*/
 }
 
Comment
Add comment · Show 1
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 PrisVas · Jun 28, 2016 at 01:28 AM 0
Share

Have you tried using debug.log to see if it's entering the OnTriggerEnter block?

Where IsThisPickupable set to true?

2 Replies

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

Answer by TBruce · Jun 29, 2016 at 02:00 AM

Hi importguru88,

I downloaded your project. I made a couple of obvious fixes

  1. 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.

  2. 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."

Comment
Add comment · Show 4 · 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 importguru88 · Jun 29, 2016 at 06:38 PM 0
Share

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.

avatar image importguru88 · Jun 29, 2016 at 06:47 PM 0
Share

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

avatar image importguru88 importguru88 · Jun 29, 2016 at 06:50 PM 0
Share

I have it to where the player is taking damage from the cube when entering.

avatar image TBruce importguru88 · Jun 29, 2016 at 09:05 PM 0
Share

Take a look at this update to your project. Here are the updates made

  1. $$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.

  2. $$anonymous$$ade the "Sparkle Rising" area heal the player based on the "Heel up speed" if the players health is below 1000.

  3. 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.

  4. 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.

avatar image
0

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.

Comment
Add comment · Show 4 · 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 Plattinator · Jun 28, 2016 at 04:36 AM 0
Share

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).

avatar image wesleywh Plattinator · Jun 28, 2016 at 11:32 PM 0
Share

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.

avatar image importguru88 wesleywh · Jun 29, 2016 at 12:45 AM 0
Share

How should code it exactly ?I did this.component health bar. I gotten error saying its not in the scene .

Show more comments

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

54 People are following this question.

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

Related Questions

How do I load scene using scene manager Set Active Scene build index / ManagerScenes are not loading Build index 0 Answers

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


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