• 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
2
Question by simpleone · May 13, 2011 at 04:15 PM · scenerestart

Reload Current Level when I die

Hey, I got a script for health , dead replacement and so on but I wanna add a few lines that makes the scene to restart when I die... here is the script I got:

var maximumHitPoints = 200.0; var hitPoints : float; var regenerationSpeed : float = 5; var painLittle : AudioClip; var painBig : AudioClip; var die : AudioClip; var deadReplacement : Rigidbody; private var gotHitTimer = -1.0; var healthGUI : GUITexture; private var healthGUIWidth = 100.0; var damage : GameObject; var explShake : GameObject;

function Awake () { healthGUIWidth = healthGUI.pixelInset.width; }

function PlayerDamage (damage : int) { if (hitPoints < 0.0) return;

// Apply damage hitPoints -= damage;

// Play pain sound when getting hit - but don't play so often if (Time.time > gotHitTimer && painBig && painLittle) { // Play a big pain sound if (hitPoints < maximumHitPoints 0.2 || damage > 100) { audio.PlayOneShot(painBig, 1.0 / audio.volume); gotHitTimer = Time.time + Random.Range(painBig.length 2, painBig.length 3); } else { // Play a small pain sound audio.PlayOneShot(painLittle, 1.0 / audio.volume); gotHitTimer = Time.time + Random.Range(painLittle.length 2, painLittle.length * 3); } }

// Are we dead? if (hitPoints < 0.0) Die();

}

function Die () { if (die && deadReplacement) AudioSource.PlayClipAtPoint(die, transform.position); var dead : Rigidbody = Instantiate(deadReplacement, transform.position, transform.rotation);

// Disable all script behaviours (Essentially deactivating player control)
var coms : Component[] = GetComponentsInChildren(MonoBehaviour);
for (var b in coms) {
    var p : MonoBehaviour = b as MonoBehaviour;
    if (p)
        p.enabled = false;
}
yield WaitForSeconds(2.0);

}

function LateUpdate () { UpdateGUI(); }

function Update (){ //REGENERATION and damage effect if (hitPoints < 200.0 && hitPoints > 0.0) hitPoints += Time.deltaTime * regenerationSpeed; damage.guiTexture.enabled = true;

if (hitPoints > 198.0)
damage.guiTexture.enabled = false;

}

function Exploasion(){ explShake.animation.Play("exploasion"); }

function UpdateGUI () { var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints); healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction; }

Could someone easy just edit the script so when I die the scene I'm currently playing will like restart after 5 seconds or something, I would really be grateful if someone took the time to it.

Comment
Add comment
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

2 Replies

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

Answer by Dylan Cristy · May 13, 2011 at 04:39 PM

At the end of your "Die" function, just add:

yield WaitForSeconds(5.0);  // or however long you want it to wait
Application.LoadLevel(Application.loadedLevel);
Comment
Add comment · Show 2 · 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 TheShadyColombian · Oct 13, 2014 at 01:27 AM 0
Share

it tells me the opening bracket is an unexpected symbol

avatar image c00pala · Oct 13, 2014 at 05:26 AM 1
Share

Hey Juan.

That's likely because you're missing some punctuation a few lines above. Have a look and make sure you haven't missed anything leading up to 'Application.LoadLevel'.

If you're still stuck, recommend posting your script/asking in a new question (as this one is fairly old) and someone might be able to give you a hand.

avatar image
4

Answer by nexis717 · Apr 12, 2016 at 10:53 PM

Just to add an updated answer to this.

You need to first include

 using UnityEngine.SceneManagement;

Then, I would create a function, something along the lines of

 public void restartCurrentScene()
     {
         int scene = SceneManager.GetActiveScene().buildIndex;
         SceneManager.LoadScene(scene, LoadSceneMode.Single);
     }

You can call this function from a UI button or, upon death. The "LoadSceneMode.Single" piece unloads all current GameObjects, before reloading the scene.

Comment
Add comment · Show 2 · 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 monrrah · Aug 25, 2016 at 05:27 AM 0
Share

hello , I am Brazilian sorry if I can not express myself properly.

When I try to do this , the scene restarts , however nothing works , just the sound . Can you please help me?

avatar image ArtOfWarfare · Oct 10, 2016 at 03:53 PM 0
Share

For some reason, ambient lightning seems to break when you do this.

$$anonymous$$y solution (if you can call it that) is to just go into Window > Lightning > Scene and set the Environment Lightning Skybox to None. Then tweak actual lights in your scene to your liking.

A better solution would be to find out what the ambient light settings are before you load the scene, and set it after the scene is loaded. I couldn't find out how to do that, though... I tried checking RenderSettings.skybox but that seems to be unchanged by reloading the scene.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Simple Restart not working 0 Answers

Disappear objects and that these are still missing when you return to load the scene 1 Answer

Resetting a Scene 2 Answers

1 Scene game|| Reset variables 1 Answer

Restarting scene not working properly 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