how to switch scenes when you die in unity 3d

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using UnityEngine.SceneManagement;

//Load based on name of scene
public class SceneScript {
  public void ChangeScene(string scene = "") {
    SceneManager.LoadScene(sceneName:"scenes Name");
  }
}

4.17
6
Boom 55 points

                                    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class BallCollision : MonoBehaviour
{
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.name == "Ball Red") {
            SceneManager.LoadScene("Scene 2");
        }
    }
}

4.17 (6 Votes)
0
0
0
Trixie G. 100 points

                                     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);
         }
     }*/
 }

0
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source