Damage and Health Script help!

I’m making a basic block game and I need those blue blocks to instantly take down your health but it seems like I just can’t get that to open

Players Health

public class PlayerHealth : MonoBehaviour {
	public int maxHealth = 100;
	public int curHealth = 100;
	
	public float healthBarLength;
	
	
	void Start () {
		healthBarLength = Screen.width / 2;
	}
	
	
	void Update () {
		AddjustCurrentHealth(0);
		
	}
	
	
	void OnGUI(){
		GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth);	
	}
	
	
	public void AddjustCurrentHealth(int adj) {
		curHealth += adj;	
		
		if(curHealth < 0)
			curHealth = 0;
		
		if(curHealth > maxHealth)
			curHealth = maxHealth;
		
		if(maxHealth < 1)
			maxHealth = 1;
		
		healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
	}
}

damage

using UnityEngine;
using System.Collections;

public class enemyscript2 : MonoBehaviour {
	public GameObject target;
	public float attackTime;
	public float coolDown;
	
	
	
	void Start () {
		attackTime = 0;
		coolDown = 0f;
		
	}
	
	
	void Update () {
		if(attackTime > 0)
			attackTime -= Time.deltaTime;
		
		if(attackTime < 0)
			attackTime = 0;
		
		
		if(attackTime == 0) {
			Attack();
			attackTime = coolDown;
		}
		
	}
	
	private void Attack() {
		float distance = Vector3.Distance(target.transform.position, transform.position);
		
		
		Vector3 dir = (target.transform.position - transform.position).normalized;
		float direction = Vector3.Dot(dir, transform.forward);
		
		
		if(distance < 1.2f) {
			if(direction > 0) { 
				PlayerHealth eh = (PlayerHealth)target.GetComponent("PlayerHealth");
				eh.AddjustCurrentHealth(-100);
			}
		}
	}
}

Right now it only damages the cube when you touch the middle of the cube it needs to touch anywhere and be damaged.

Would it make more sense to use collision objects and if facing the right direction and if objects are within each other, then modify health? I’m also confused about the part of the code where it’s making the enemy point toward the player - it seems like the ‘enemies’ in the screenshots are some kind of obstacle, not like something chasing it? If you want to just make health go down as it enters those blue blocks, here’s what I would do:

Make colliders for each. Oh also, I use javascript so please bear with.

function onTriggerEnter(collided : Collider) {
    if (collided.name == 'blueBlock') {
        health -= collided.gameObject.GetComponent(whateverScript).damage;
        // And then maybe some of that other timing cool down stuff here if needed
    }
}

I’m not sure how to script in Java, I am much familiar with C# but how shall I convert this?

My best guess after some quick searching - c# people, please lmk if I’m wrong.

Script on the player:

using UnityEngine;
using System.Collections;

public class playerStuff : MonoBehaviour {

    float health = 100.0;

    void OnTriggerStay(Collider collided) {
        // Not sure if I can do -= in c#
        health = SubtractDamage(health);
    }

    float SubtractDamage(float val) {
        float newVal;
        // Perhaps here call a function on the enemy to get the amount of damage
        newVal = val - 1.0;
        return newVal;
    }
}

Edit: I keep switching between OnTiggerStay and OnCollisionStay - I don’t fully understand the difference yet. I use Trigger.

I’m getting a parsing error let me see if I can figure out why

using UnityEngine;

using System.Collections;

 

public class playerStuff : MonoBehaviour {

 

    float health = 100.0;

 

    void OnTriggerStay(Collider collided) {

        // Not sure if I can do -= in c#

        health = SubtractDamage();

    }

 

    float SubtractDamage(float val) {

        float newVal;

        // Perhaps here call a function on the enemy to get the amount of damage

        newVal = val - 1.0;

        return newVal;

    }
}

Ah yes, the ‘}’. Does it work after that? I have not tested any of this sorry. Am at work.

I’m trying to correct the errors

Errors

I made a separate subtraction function because an example for multiplying I saw did the same. I assume I can’t do +=, -=, etc, but is it not possible to just say val = val - 1.0 in c#? Again, not in an environment where I can test and I don’t use c# just curious.

Oops, first of all,

health = SubtractDamage();

should be

health = SubtractDamage(health);

Not sure of the other error

I don’t think so I’m sure it’d be more like if (val x=0) or something of the sort

Did passing health into the function as above help? As for the other error, perhaps try changing all the floats to double and see what happens. I honestly am confused as to where the double is coming in at all, but it’s incompatible with the float. Maybe the 1.0 is the double. Not sure how to make that a float if that’s the case. Maybe see what happens if you keep the floats and just subtract 1 as an integer.

Yeah it did but I can’t fix the second error. How would you write this in javascript?

Nah, you are more familiar with c# let’s try to fix it there. Don’t give up! :slight_smile:

The second error you are getting is with assigning/modifying floats with constants. 100.0 and 1.0 are doubles by default. To make them floats, add the suffix ‘f’ to the number like so: 100.0f Here is the revised code - still not tested, but hopefully without those errors.

using UnityEngine;
using System.Collections;

public class playerStuff : MonoBehaviour {

    float health = 100.0f;

    void OnTriggerStay(Collider collided) {
        // Not sure if I can do -= in c#
        health = SubtractDamage(health);
    }
 
    float SubtractDamage(float val) {
        float newVal;
        // Perhaps here call a function on the enemy to get the amount of damage
        newVal = val - 1.0f;
        return newVal;
    }
}

Also note this is going to take health every frame. You’ll want to make it time based or you may get different health drain on differently performing machines.

I’m not getting errors but I have to add a debug to make sure it’s working?

void OnGUI(){
		GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth);	
	}

This is from the script above would I just change curHealth?

Sure - as long as it’s in the same class. Note that you’ll also need to make sure maxHealth is declared in the class. Also, are you able to concatenate strings and floats in c#? If not, use .ToString() to turn those floats into strings on printing.

Also, for debugging I rarely put it to the gui. I often print statements or just look at the value in the inspector. I don’t have to worry about public and private in javascript if I don’t implicitly set it (all my variables show in the inspector) but I think I may have read that only public c# variables will show up in the inspector. Again, not sure about that.