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 GalaxxyGaming · Aug 08, 2018 at 12:15 PM · playerdestroyscene-loading

How to cite a specific object in code c# / reloading a scene

I am a new young programmer that is working on a game, and i ve faced this issue since a day and i cant find a solution for it. So I came here seeking for help.

I made a script for damaging both the player and the enemy at the same time:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Damageable : MonoBehaviour {
 
     public float maxHealth;
     private float health;
     public Image healthBar;
     void Start()
     {
         health = maxHealth;
     }
 
     void Update()
     {
         if (health > maxHealth)
         {
             health = maxHealth;
         }
         if (health <= 0)
         {
 
             Destroy(gameObject);
             
         }
     }
 public void Damage ( int damageValue)
     {
         health -= damageValue;
         healthBar.fillAmount = health / maxHealth;
         Debug.Log(health);
     }
 }

It works properly, but i want the scene to reload on player death. i tried adding: Application.LoadedLevel (Application.loadedLevel); but now not only when the player dies the scene reloads, but also when the enemy is killed. I want to find a way to specify that only only when the player dies the scene reloads. It will be great if any of u pro coders will help me. :D

P.S. dont mind the damageValue thingies cuz they are in another script.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by ShadyProductions · Aug 08, 2018 at 12:25 PM

Add a bool field

 public bool IsPlayer;

then in the editor find the damageable script on the player and set the IsPlayer field to true and check for it when u destroy the gameobject.

 if (health <= 0)
 {
       if (IsPlayer)
       {
              SceneManager.LoadScene(scenename);
       }
       else 
       {
              Destroy(gameObject);    
       }
 }
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 GalaxxyGaming · Aug 08, 2018 at 02:03 PM 0
Share

Thank you sir, but i have one last question: do i need to put at the top using UnityEngine.Scene$$anonymous$$anager; or no need?

avatar image GalaxxyGaming · Aug 08, 2018 at 02:07 PM 0
Share

never $$anonymous$$d my last comment, it tells me when i dont put using UnityEngine.Scene$$anonymous$$anager; that there is no such as Scene$$anonymous$$anager. and when i put it, it tells me UnityEngine doesnt contain Scene$$anonymous$$anager

avatar image GalaxxyGaming · Aug 08, 2018 at 02:09 PM 0
Share

And how do we do this exactly:"and set the IsPlayer field to true and check for it when u destroy the gameobject." Can you be more precise please. i Really appreciate your help

avatar image ShadyProductions GalaxxyGaming · Aug 09, 2018 at 07:01 AM 0
Share

I assume your script is placed on every enemy and player object in the scene yes? So if you click on the player gameobject in the inspector you should see the 'IsPlayer' checkbox in the properties windows of that gameobject, you can enable it there. And then just put that piece of code that I shared into Update method. And Scene$$anonymous$$anager is based on your unity version, its for the newer versions, you are probably using an old version of unity so you have to use Application.LoadLevel

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

112 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 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 avatar image avatar image avatar image avatar image

Related Questions

Should I reload scene after player dies or make custom system to handle that? 0 Answers

Enemies cannot kill Player. 0 Answers

how to load scenes so that my location is saved 0 Answers

References to Prefab get lost on start 1 Answer

Respawn fix 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