- Home /
Help me fix this basic HP script
I'm working on a basic HP script (copying the one found in this video - http://www.youtube.com/watch?v=-3c9Eohwdvw - just with some modified names). However, when I'm in game the object I shoot isn't being destroyed once it's taken 'lethal' damage.
Here is the HasHealth.cs
snippet:
using UnityEngine;
using System.Collections;
public class HasHealth : MonoBehaviour {
public float objectHitPoints = 100f;
public void receiveDamage (float damageAmount) {
// Decrement the HP each time damage is taken
objectHitPoints -= damageAmount;
// When the object has no more HP...
if (objectHitPoints <= 0) {
Die();
}
}
void Die (){
Destroy (gameObject);
}
}
and here is the excerpt from my Player_Shoots.cs
file.
using UnityEngine;
using System.Collections;
public class Player_Shoots : MonoBehaviour {
public float damage = 50f;
public GameObject bulletPrefab;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0) && clipSize > 0) {
Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
RaycastHit hitInfo;
// Raycast to detect hits
if(Physics.Raycast(ray, out hitInfo)){
Vector3 hitPoint = hitInfo.point;
// Deal damage to target, IF it has health
HasHealth h = gameObject.GetComponent<HasHealth>();
if (h != null) {
h.receiveDamage(damage);
}
// Instantiate our bullet at the hitPoint
if(bulletPrefab != null){
GameObject bullet = (GameObject)Instantiate(bulletPrefab, hitPoint, Camera.main.transform.rotation);
bullet.rigidbody.AddForce(0, 400 , 0);
}
}
}
}
}
I've also attached a screenshot of the Inspector panel of my target cube - the object that should be destroyed once shot twice.
Hi, please edit the title of this post so users can see what you are asking for specifically. Also, please write where you think the problem lies and what you've attempted to do to try and fix it.
Hi, using Debug.Log("some text telling where you are in the execution of the code"); you can check what condition was true or false: it will help you narrow the problem and find why/where it doesn't work. Debug.DrawLine or .DrawRay is also very useful to display rays or vectors and see if everything is where is should be.
Answer by Landern · Oct 14, 2014 at 02:12 PM
If Player_Shoots.cs is attached to your player and HasHealth.cs is attached to the cube(what the player object is firing at), then your code is trying to get your HasHealth.cs from the player and not the gameobject the raycast it.
Current update with possible issue:
void Update () {
if (Input.GetMouseButtonDown (0) && clipSize > 0) {
Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
RaycastHit hitInfo;
// Raycast to detect hits
if(Physics.Raycast(ray, out hitInfo)){
Vector3 hitPoint = hitInfo.point;
// Deal damage to target, IF it has health
HasHealth h = gameObject.GetComponent<HasHealth>(); //<-- Your problem, gameObject refers to the gameObject that this is attached to.
if (h != null) {
h.receiveDamage(damage);
}
// Instantiate our bullet at the hitPoint
if(bulletPrefab != null){
GameObject bullet = (GameObject)Instantiate(bulletPrefab, hitPoint, Camera.main.transform.rotation);
bullet.rigidbody.AddForce(0, 400 , 0);
}
}
}
}
Make sure you're referencing the gameobject that was hit that has the HasHealth script attached. I'm basing this off the screen shot you included and making assumptions:
void Update () {
if (Input.GetMouseButtonDown (0) && clipSize > 0) {
Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
RaycastHit hitInfo;
// Raycast to detect hits
if(Physics.Raycast(ray, out hitInfo)){
Vector3 hitPoint = hitInfo.point;
// Deal damage to target, IF it has health
HasHealth h = hitInfo.collider.gameObject.GetComponent<HasHealth>();
if (h != null) {
h.receiveDamage(damage);
} else {
Debug.Log("Couldn't Get HasHealth Script after raycast hit");
}
// Instantiate our bullet at the hitPoint
if(bulletPrefab != null){
GameObject bullet = (GameObject)Instantiate(bulletPrefab, hitPoint, Camera.main.transform.rotation);
bullet.rigidbody.AddForce(0, 400 , 0);
}
}
}
}
Also adding some Debug.Log statements can help you narrow thing down a bit. I added one just in case HasHealth couldn't be found.
Thank you SO much for your help, I really appreciate it.
It's weird that my behaviour was so different to that in the video, but I'm going to write that off as different versions of Unity?
Your answer
Follow this Question
Related Questions
accuracy problem with ray cast 2 Answers
RayCast hit point. 0 Answers
How should I make a custom suspension system? 1 Answer
Draw Line In GameView Point to point 1 Answer