Changing Float in another script?

I’m trying to make the player lose some HP when he collides with a certain area, but i can’t seem to be able to quite do it. I’m able to make it play a sound on collision but i can’t get it to change the float in another script. Here are my scripts(C#):

    using UnityEngine;
    using System.Collections;
    
    public class Collision : MonoBehaviour {
    
    	public GameObject pHP;
    	
    	void OnTriggerEnter(Collider other) {
    		audio.Play();
    		pHP.Health = -10;
    	}
    	
    	// Use this for initialization
    	void Start () {
    	pHP = GameObject.Find("ThePlayer");
    	pHP.GetComponent("HP");
    	
    	}
    	
    	// Update is called once per frame
    	void Update () {
    	}
    }

and here’s the player script shortened:

    using UnityEngine;
    using System.Collections;
    
    public class HP : MonoBehaviour {
    public float Health = 100.0F;
    }

This is the error that i’m getting: error CS1061: Type UnityEngine.GameObject’ does not contain a definition for Health' and no extension method Health’ of type UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)

It looks like your OnTriggerEnter method is accessing the pH variable. Shouldn’t that be pHP?

Also, pHP.GetComponent("HP"); is a problem. You’re not assigning the return value of GetComponent to anything.

Declare a variable to hold a reference to the component…

HP hp;

Then change your GetComponent call to…

hp = pHP.GetComponent("HP");

Or better yet, use generics…

hp = pHP.GetComponent< HP >();

Now you can reference the Health property: hp.Health -= 10;

Ive been trying the same thing on an object shooting a raycast, so I added Debug log to show current health float on my enemies script, the value does decrease but it instantly snaps back to the default value.

Heres my script for ememy…

using UnityEngine;
using System.Collections;

public class enemy_detect : MonoBehaviour {

	public float scensorreach = 3f; 
	public float damage;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

		GameObject controller = GameObject.Find ("Player");
		Health HP = controller.GetComponent <Health> ();
		float currenthealth = HP.health; 

		Vector3 scensorrenderFwd = transform.TransformDirection(Vector3.right) * scensorreach;
		Debug.DrawRay (transform.position, scensorrenderFwd, Color.red);
		///___________________________________///
		
		Vector3 scensordetectfwd = transform.TransformDirection (Vector3.right);
		bool scensorfwd = Physics.Raycast (transform.position, scensordetectfwd, scensorreach);
		if (scensorfwd) {
						currenthealth += -1;
						if (currenthealth < 100)
								damage += 1 * Time.deltaTime;
						if (currenthealth == 0)
								Destroy (controller);
				}
		Debug.Log (damage);
		Debug.Log (currenthealth);
	}
}

And heres the player script for the value Im trying to change,

using UnityEngine;
using System.Collections;

public class Health : MonoBehaviour{

	public float health = 100;

	private float Width = Screen.width;
	private float Height = Screen.height;

	void Start() {
	} 

	void Update() {
	}

	void OnGUI() {

	GUI.Box(new Rect(Width/2 - health * 2, 10, health * 4, 20),"HP " + health);

	}
}

if you can stop the value snapping back then it should work fine…