Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by shay4545 · Aug 09, 2014 at 02:50 AM · c#enemylives

Trouble with lives

I having an issue where when my player hit an enemy he dies as he should, but sometimes he loses 2 lives and others he only loses 1. I think the issue might be that my character has two colliders: a circle collider2D and a box Collider2D and they are both hitting the enemy. I have no idea how to fix it though. Here are my scripts (They are both attached to my player)

Lives:

  using UnityEngine;
     using System.Collections;
 
 public class Lives : MonoBehaviour {
 
 public static int PlayerLives = 5;
 public int MaxLives = 999;
 public GUISkin Life;
 public Rect LifeDisplay;
 
 
 void Update ()
 {
     if(PlayerLives >= MaxLives)
     {
         PlayerLives = MaxLives;
     }
     if(PlayerLives <= 0)
     {
         Application.LoadLevel(0); //Gameover Level
     }
     PlayerPrefs.SetInt("Lives", PlayerLives);
     PlayerLives = PlayerPrefs.GetInt("Lives");
 }
 
 
 void OnGUI()
 {
     GUI.skin = Life;
     GUI.Label(LifeDisplay, "Lives: " + PlayerLives.ToString(), GUI.skin.GetStyle("Lives"));
 }
 }



Enemy:

 using UnityEngine;
 using System.Collections;
 
 public class Enemy : MonoBehaviour {
 
 public string Level = "";
 
 
     void OnTriggerEnter2D(Collider2D enemy)
     {
         if(enemy.tag == "Enemy")
         {
             Lives.PlayerLives--;
             Application.LoadLevel(Level);
         }
     }
 }



Any help would be much appreciated.

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
3
Best Answer

Answer by rutter · Aug 09, 2014 at 02:52 AM

Sounds like you need your player to become invulnerable for a short period of time after they're hit.

For example:

 //keep track of next time we can be hurt
 float nextVulnerableTime = 0f;

 //after being hit, we'll be invulnerable for this many seconds
 float invulnSeconds = 5f;

 void TakeDamage() {
     if (Time.time > nextVulnerableTime) {
         nextVulnerableTime = Time.time + invulnTime;
         //subtract life or whatever needs to be done
     }
 }

As an alternative, you could make sure to always distinguish which collider you hit, and make sure that an enemy is disabled after it hits one of them.

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 shay4545 · Aug 09, 2014 at 03:17 PM 0
Share

Thanks! that fixed it. I would upvote your answer but I dont have 15 rep yet.

avatar image kdarius43 · Aug 11, 2015 at 08:53 PM 0
Share

this makes sense but what do you turn off on the player so they cant get hit for that time period? The collider?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do I make an event that occurs only when application first launched? 2 Answers

GUI trouble 1 Answer

C# Third Person Controller CS1061 0 Answers

Make player not be seen by AI, when player in foilage and shadows. 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