- Home /
Question by
GroundstompStudios · May 20, 2019 at 12:04 AM ·
c#scripting problemunityeditor
How to make these scripts work together?
I am trying to make it so this script Fallout and PlayerHealth work together. Currently, when the player gets hit by an enemy, it just dies and doesn't respawn, but I want the same system as my fallout thing. I basically want to replace the if (currentHealth <= 0 && !dead) Die(); to something like the fallout system. Here are the scripts.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour
{
public int startingHealth = 100;
public int currentHealth;
public GameObject player;
bool dead;
bool damaged;
[Header("UI Components")]
public Text healthText;
public Fallout fallout;
private void Awake()
{
currentHealth = startingHealth;
}
private void Update()
{
healthText.text = currentHealth.ToString();
damaged = false;
}
public void TakeDamage (int amount)
{
damaged = true;
currentHealth -= amount;
if (currentHealth <= 0 && !dead)
Die();
}
void Die()
{
dead = true;
Destroy(player);
print("Player died.");
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Fallout : MonoBehaviour
{
public Transform spawnPoint;
public int lives = 3;
public GameObject player;
public GameObject respawnMenu;
public GameObject maxHealthText;
public GameObject healthText;
public Text livesText;
public GameObject maxLives;
public GameObject livesTextObj;
public bool dead;
private void Update()
{
livesText.text = lives.ToString();
}
public void FalloutDeath(Collision2D collision)
{
if (collision.transform.CompareTag("Player"))
{
collision.transform.position = spawnPoint.position;
lives -= 1;
}
if (lives < 0)
{
respawnMenu.SetActive(true);
maxHealthText.SetActive(false);
healthText.SetActive(false);
livesTextObj.SetActive(false);
maxLives.SetActive(false);
Die();
}
else
{
respawnMenu.SetActive(false);
maxHealthText.SetActive(true);
healthText.SetActive(true);
livesTextObj.SetActive(true);
maxLives.SetActive(true);
}
}
void Die()
{
dead = true;
Destroy(player);
print("Player died.");
}
}
Comment
Hello.
dont understand what are you asking... You want to merge these 2 scripts?
Then, try ti, do it, and all you cant achieve, come to ask... But dont ask even before try it...
Bye!
Your answer
Follow this Question
Related Questions
No Monobehaviour scripts in files 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Respawn script not working. 2 Answers