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 kategames · Nov 24, 2019 at 11:49 PM · #pragma#pragma strict

Help, Losing lives too easily

Hello, I'm Kate and I'm entering the world of game developer and been having dificulties for 2 weeks now, if anyone can help me I'll be very grateful.:)

I'm making a 3D Space Ship game and I'm having a hard time in the Hero, Enemy and Boss life. When I press play:

1 - Enemies take damage and die - Ok 2 - Boss takes damage, dies but doesn't load new scene - :( 3 - And the Hero takes only 1 damage and dies, even though I put 3 lives for him. :( (In HUD of 3 goes to 0 of lives)

The script is in: Hero Boss Enemy

Hero = 3 lives Boss = 10 lives Enemy = 2 lives


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

public class Destroy_of_Object_contact : MonoBehaviour { public GameObject enemyexplosion; public GameObject Ship_Hero;

 public int PointValue;
 public int LiveHero;
   
 private GameControl gamecontrol;  

 public int LiveEnemy;
 public int Damage;

 void Start() 
 {
     GameObject GameControlobject = GameObject.FindWithTag ("GameController");
     if (GameControlobject != null)
     {
        gamecontrol = GameControlobject.GetComponent <GameControl>();
     }
     if (gamecontrol == null)
     {
         Debug.Log ("Cannot find 'ControleDoGame' script");
     }
     
 }

 private void OnTriggerEnter(Collider Object) 
 {

     if (Object.tag == ("Shooting Barriers") || Object.CompareTag("Enemy") || Object.CompareTag("Boss"))
     {
        return;
     }

     if (enemyexplosion != null)
     {
        
         LiveEnemy = LiveEnemy - Damage;

         if (LiveEnemy <= 0)
         {
              
             gamecontrol.AddPoints(PointValue);
             Destroy(Object.gameObject);
             Destroy(gameObject);

         }

         Instantiate(enemyexplosion, transform.position, transform.rotation);

     }

     if (Object.tag == "Boss") 
     {
         
         if (LiveEnemy <= 0)
         {

             SceneManager.LoadScene("NewScene");
             
         }

         Instantiate(enemyexplosion, transform.position, transform.rotation);
     }

     if (Object.tag == "Player") 
     {
         gamecontrol.RemoveLives(LiveHero);
         if (LiveHero <= 0)
         {
             SceneManager.LoadScene("GameOver");
             Destroy(Object.gameObject);
             Destroy(gameObject);

         }

         Instantiate(Ship_Hero, Object.transform.position, Object.transform.rotation);
     }

 }

}

/* GameControl:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class GameControl : MonoBehaviour {

 public Text pointsTextUI;
 private int Points;

 public Text LiveTextUI;
 public int Live;

 void Start()
 {
     string formatTextStart = string.Format("{0:00000000}", Points);
     pointsTextUI.text = formatTextStart;

     string LiveTextStart = string.Format("3", Live);
     LiveTextUI.text = LiveTextStart;

 }

 public void AddPoints(int NewPoints)
 {
     Points += NewPoints;
     UpdateScore();
 }

 void UpdateScore()
 {
     string formatText = string.Format("{0:00000000}", Points);
     pointsTextUI.text = formatText;
 }

 public void RemoveLives(int LessLife)
 {
     Live--;
     Lives();
 }

 public void Lives()
 {
     string formatTextLive = string.Format("0", Live);
     LiveTextUI.text = formatTextLive;
 }

} */

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

Answer by goutham12 · Nov 25, 2019 at 09:51 AM

It's looks like you are creating one script for both enemy and player. i have found one problem in your script i.e after colliding you are not destroying the collided object(I mean Destroy(other.gameObject) you missed this line in above class. Actually i can't understand what you are doing there. but let me give a quick example for how you can do. attatch the below script to the player

public class PlayerCollisionController : MonoBehaviour {

 private GameControl gamecontrol;

 void Start()
 {
     GameObject GameControlobject = GameObject.FindWithTag("GameController");
     if (GameControlobject != null)
     {
         gamecontrol = GameControlobject.GetComponent<GameControl>();
         gamecontrol.livesHero = 3;
     }
     if (gamecontrol == null)
     {
         Debug.Log("Cannot find 'ControleDoGame' script");
     }

 }

 private void OnTriggerEnter(Collider other)
 {
     if(other.tag == "enemy")
     {
         Destroy(other.gameObject);
         gamecontrol.livesHero -= 1;
         if(gamecontrol.livesHero <= 0)
         {
             SceneManager.LoadScene("GameOver");
         }
     }
 }

}

Note: you need a variable in gamecontroller to hold player lives i.e livesHero

Comment
Add comment · Show 1 · 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 kategames · Nov 28, 2019 at 01:36 AM 0
Share

Thank You very much! It worked! :)

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

187 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

Related Questions

Working Computer UI. 0 Answers

How To Show a Question UI Canvas After Collider ? 1 Answer

not run app again when read nfc tag 0 Answers

Unity Package Manager Error 0 Answers

AI walk around and flashlight stop 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