- Home /
Health won't work.
Hi. I'm making a game which requires me to have a healthscript and a seperate damage script. I tried following a tutorial from unity. But it won't work. I am still new at this, although I know a little bit. So can anyone please help me? Just try to tell me what's wrong because I've no clue. I would be so grateful.
Here's my Health Script
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class HealthScript : MonoBehaviour {
public int startingHealth = 3;
public int currentHealth;
public Slider healthSlider;
void Awake ()
{
currentHealth = startingHealth;
}
void Update ()
{
}
public void TakeDamage (int amount)
{
// Reduce the current health by the damage amount. cuz that has to happen
currentHealth -= amount;
// Set the health bar's value to the current health. cuz that's cool (amirite?)
healthSlider.value = currentHealth;
if(currentHealth <= 0)
{
// ... it should die. so please die
Destroy (gameObject);
Debug.Log ("Yesh this works.");
}
}
}
And this is my Move/Damage script. Yes it's a bit messy. sorry
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class moveP1 : MonoBehaviour {
//Move and wall stuff
public KeyCode upKey;
public KeyCode downKey;
public KeyCode rightKey;
public KeyCode leftKey;
public float speed;
public GameObject wallPrefab;
public GameObject BulletPrefab;
Collider2D wall;
Collider2D Bullet;
Vector2 lastWallEnd;
public float orthographicSize;
//health stuff
HealthScript playerHealth;
GameObject player;
public int attackDamage;
public GameObject other;
bool isHit = false;
bool bulletHit = false;
void Start ()
{
player = GameObject.FindGameObjectWithTag ("player");
playerHealth = player.GetComponent <HealthScript> ();
Camera.main.orthographicSize = 80;
GetComponent<Rigidbody2D>().velocity = Vector2.up * speed;
spawnWall();
InvokeRepeating ("BulletSpawner", 1f, 3f-(Time.deltaTime));
// health stuff
}
void Update ()
{
//move
if (Input.GetKeyDown (upKey)) {
GetComponent<Rigidbody2D> ().velocity = Vector2.up * speed;
spawnWall ();
} else if (Input.GetKeyDown (downKey)) {
GetComponent<Rigidbody2D> ().velocity = -Vector2.up * speed;
spawnWall ();
} else if (Input.GetKeyDown (rightKey)) {
GetComponent<Rigidbody2D> ().velocity = Vector2.right * speed;
spawnWall ();
} else if (Input.GetKeyDown (leftKey)) {
GetComponent<Rigidbody2D> ().velocity = -Vector2.right * speed;
spawnWall ();
}
fitColliderBetween (wall, lastWallEnd, transform.position);
//Health?
if (isHit)
{
playerHealth.TakeDamage (3);
isHit = false;
}
if(bulletHit)
{
playerHealth.TakeDamage (1);
Destroy (other );
bulletHit = false;
}
//Loading levels
if (GameObject.Find("player 1") )
{
//Debug.Log ("Don't die!");
}
else
{
Application.LoadLevel("player 2 won");
}
if (GameObject.Find("player 2") )
{
//Debug.Log ("Don't die!");
}
else
{
Application.LoadLevel("player 1 won");
}
}
void BulletSpawner()
{
GameObject rr = (GameObject)Instantiate (BulletPrefab, new Vector3(5, 0, 0), Quaternion.Euler(0,0,Random.Range(0,360)));
Bullet = rr.GetComponent<Collider2D> ();
}
void spawnWall()
{
lastWallEnd = transform.position;
GameObject g = (GameObject)Instantiate(wallPrefab, transform.position, Quaternion.identity);
wall = g.GetComponent<Collider2D>();
}
void fitColliderBetween(Collider2D co, Vector2 a, Vector2 b)
{
co.transform.position = a + (b - a) * 0.5f;
float dist = Vector2.Distance(a, b);
if (a.x != b.x)
co.transform.localScale = new Vector2(dist + 1, 1);
else
co.transform.localScale = new Vector2(1, dist + 1);
}
void OnTriggerEnter2D(Collider2D co)
{
if (co != wall)
{
isHit = true;
}
if (co = Bullet)
{
bulletHit = true;
}
}
}
Answer by SamTheT · Apr 22, 2015 at 06:01 PM
you can make an instance of the healthscript in your damage script:
HealthScript myHealth = GameObject.FindOfObjectOfType<HealthScript>();
that probably will solve it
Do I have to write something in the () after FindObjectOfType?
Your answer
Follow this Question
Related Questions
Object reference not set to an instance of an object 1 Answer
Health Bar Only For Falling Damage 2 Answers
blood damage like call of duty? 2 Answers
Health Bar Help 2 Answers