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;
}
} */
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
Your answer
Follow this Question
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