I got a bunch of problems and i don't know how to fix them!
I'm making a game where i have to protect a base from being destroyed. However i have problems with the rest of the code what i mean is they don't work! One, the enemies won't collide with the base which is supposed to make them disappear and take one of the base's lives each. Two, GameOver text won't appear when the lives are gone and three the keycode to restart the game does not work either. Is there a way all this can be fixed?
public class GameManager : MonoBehaviour
{
public bool gameHasEnded = false;
public string gameState = "Play";
public bool isplayable = true;
public GameObject GameOver;
public GameObject refToBase, refToPlayer, refToEnemy, refToEnemy2, refToCannon;
public GameObject Lives, Lives2, Lives3;
public int LifeNum;
// Start is called before the first frame update
void Start()
{
GameOver.gameObject.SetActive(false);
LifeNum = 3;
Lives.gameObject.SetActive(true);
Lives2.gameObject.SetActive(true);
Lives3.gameObject.SetActive(true);
refToCannon = GameObject.Find("Cannon");
}
// Update is called once per frame
void Update()
{
if (LifeNum > 3)
{
LifeNum = 3;
}
switch (LifeNum)
{
case 3:
Lives.gameObject.SetActive(true);
Lives2.gameObject.SetActive(true);
Lives3.gameObject.SetActive(true);
break;
case 2:
Lives.gameObject.SetActive(true);
Lives2.gameObject.SetActive(true);
Lives3.gameObject.SetActive(false);
break;
case 1:
Lives.gameObject.SetActive(true);
Lives2.gameObject.SetActive(false);
Lives3.gameObject.SetActive(false);
break;
case 0:
Lives.gameObject.SetActive(false);
Lives2.gameObject.SetActive(false);
Lives3.gameObject.SetActive(false);
gameHasEnded = true;
Time.timeScale = 0;
break;
}
if (gameHasEnded == true)
{
gameState = "Stop";
refToPlayer.GetComponent<Player>().PlayerState = "Stop";
refToCannon.GetComponent<Player>().PlayerState = "Stop";
isplayable = false;
GameOver.gameObject.SetActive(true);
if (Input.GetKey(KeyCode.R))
{
Restartgame();
}
}
}
void Restartgame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
public class Enemy2 : MonoBehaviour
{
public GameObject refToBase;
public GameObject refToGameManager;
public float moveSpeed;
public string Enemystate;
private Transform target;
private int health = 1;
// Start is called before the first frame update
void Start()
{
moveSpeed = 0.6f;
target = GameObject.FindGameObjectWithTag("Threat").GetComponent<Transform>();
}
// Update is called once per frame
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Cannonball"))
{
Destroy(collision.gameObject);
health--;
if (health <= 0)
{
Death();
}
}
if (collision.CompareTag("Threat"))
{
Destroy(this.gameObject);
refToGameManager.GetComponent<GameManager>().LifeNum -= 1;
}
}
private void Death()
{
Destroy(this.gameObject);
}
public class Base : MonoBehaviour
{
public GameObject refToGameManager;
public GameObject refToCannonball;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (this.GetComponent<SpriteRenderer>().bounds.Intersects(refToCannonball.GetComponent<SpriteRenderer>().bounds))
{
Destroy(gameObject);
}
}
private void OnTriggerEnter2D (Collider2D collision)
{
if (collision.gameObject.tag.Equals ("Enemy"))
{
Destroy(gameObject);
refToGameManager.GetComponent<GameManager>().LifeNum -= 1;
}
}
}
Your answer
Follow this Question
Related Questions
My collectable objects don't respond like they should after calling a coroutine for the first time? 0 Answers
[Unity 2D]Grid Based Character Movement Issues with Vector3D.movetowards 0 Answers
i want game to end on no collisions 0 Answers
Get player and enemy hit bars to respond to each others attack not their own 1 Answer
How to detect if not colliding 1 Answer