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 unity_T2PcF_gS6-wL-g · Jun 19, 2019 at 12:32 PM · colliderdamagespace shooterhitpoints

Need script for simple heath system

So I've been working on a Space Shooter game and for now each time my ship gets hit,my ship is destroyed. So I'm looking for some help regarding a simple script where my Ship would have say 5 health and each time it gets hit the health decreases by 1. I'm not asking for health bar GUI or something, just a simple script when attached to my ship and when any collider hits my ship it would loose 1 unit of health.

Here is my collider code so far but the update() method is not allowing me to put OnTrigger inside it. Some time the script is working for 2 sec but not everytime health returns to 5 automatically .

public class ColliderContact : MonoBehaviour { public GameObject explosion; public GameObject playerExplosion; public int scoreValue; private Done_GameController gameController; public int Health = 5;

 void Update()
 {
     
     Debug.Log(Health);
 }

 
 void Start ()
 {
     GameObject gameControllerObject = GameObject.FindGameObjectWithTag ("GameController");
     if (gameControllerObject != null)
     {
         gameController = gameControllerObject.GetComponent <Done_GameController>();
     }
     if (gameController == null)
     {
         Debug.Log ("Cannot find 'GameController' script");
     }
 }

 
  
 void OnTriggerEnter (Collider other)
 {
     

     if (other.tag == "Boundary" || other.tag == "Enemy")
     {
         return;
     }

     if (explosion != null)
     {
         Instantiate(explosion, transform.position, transform.rotation);
     }
     if (other.tag == "Player")
     {

         {

             Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
             Health--;
             gameController.GameOver();
         }
         
     }



     gameController.AddScore(scoreValue);
     Destroy (other.gameObject);
     Destroy (gameObject);
 }
 




}

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
0

Answer by yaourt · Jun 19, 2019 at 12:49 PM

Is this script attached to your Player? If so, why are you trying to detect collision on a "Player" tag? What are those lonely brackets inside the condition? Why do you call gameController.GameOver() on each collision? Does this method reset your health?

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 unity_T2PcF_gS6-wL-g · Jun 19, 2019 at 01:21 PM 0
Share

The script is attached to an empty object named gamecontroller not with the player. And the GameOver() is from another script that only enables (Boolean) when the ship is destroyed, asu$$anonymous$$g now the player has 1 unit of health. And yes this method is resting my health. I was following the Space Shooter project from unity to set the explosion and other things so they had these.

avatar image yaourt unity_T2PcF_gS6-wL-g · Jun 19, 2019 at 02:21 PM 0
Share

First thing, if this script is attached on an empty gameObject, you are trying to detect collision on an empty gameObject without any collider, so no collision neither... OnTriggerEnter will trigger only if there is a collision between the collider attached on the same gameobject than your script and an other one. One of them has to have a RigidBody attached (most part of the time it will be the player). In your case, you should probably attached this script on the player (which as health) and detect collision with Enemies, so add an enemy tag to your enemy gameObject and check for this tag on collision. If you don't understand any line you are writing, I highly recommend you to start, with an ultra-simple 2d game, or if you really really don't understand, with basics of programmation.

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

207 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 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

how to approach connecting different boss parts(gameobjects) to one health bar that calculate damage when a part gets destroyed? 0 Answers

Health bar/damage script not working. Please help. 0 Answers

Tool Durability if Hit any Collider 1 Answer

Apply more damage over time?,How can I apply more damage over time? 0 Answers

Collision not detecting 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