How do i run a function from within another script?

Hello, i think i’ve solved the issue i ahd yesterday with accessing another script, i did remake one of them so that they both are made in c#. Now i have another issue.

What i want it to do is that when the box (enemyscripts “owner”) is close enough to the player. I want it to run a damage function from within “healthbarscript”. But i gett a null reference what not shit whenever he hits my player. I have no idea of why this is as it is.

Could somehere here spot the issue?

Healthbarscript:

using UnityEngine;
using System.Collections;


public class HealthBarScript : MonoBehaviour {
	//varablar. Namnen talar för sig själva
	public int Maximumhealth = 100;
	public int CurrentHealth = 100;
	
	public float healthBarLength;
	
	// Use this for initialization
	void Start () {
		healthBarLength = Screen.width / 2;
	}
	
	// Update is called once per frame
	void Update () {
		AdjustCurrentHealth(0);
	}
	//Skapar healthboxen
	void OnGUI(){
		GUI.Box(new Rect(10, 10, healthBarLength, 20),
		        CurrentHealth + "/" + Maximumhealth);	
	}
	//Förhindrar division med 0 etc.
	public void AdjustCurrentHealth(int adj){
		CurrentHealth += adj;
		
		if(CurrentHealth < 0){
			CurrentHealth = 0;	
		}
		
		if(CurrentHealth > 100)
		{
			CurrentHealth = Maximumhealth;	
		}
		
		if(Maximumhealth < 1){
			Maximumhealth = 1;	
		}
		
		healthBarLength = (Screen.width / 2) * (CurrentHealth / (float)Maximumhealth);
	}
	//förlora hp functionen
	public void DamageFunction(int Damage)
	{
		CurrentHealth -= Damage;	
	}
}



Enemyscript:


using UnityEngine;
using System.Collections;

public class EnemyMovement2 : MonoBehaviour {
	
	//Variabler
	public Transform target;
	new int moveSpeed = 3;
	new int rotationSpeed = 3;
	
	private Transform myTransform;
	
	void Awake(){
		//Gör så att man har sin egna transform redo
		myTransform = transform;	
	}
	
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
		//Sätter varablen target till ett GameObject
		GameObject go = GameObject.FindGameObjectWithTag("Player");
		
		target = go.transform;
		//rotationen
		myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
		//rörelsen emot spelaren
		myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
		//En debug linje för att se tydligare rörelser
		Debug.DrawLine(target.transform.position, myTransform.position);
		
		
		//Mäter avståndet emellan kuben och Playern
		float distance = Mathf.Sqrt( Mathf.Pow(target.position.x-myTransform.position.x,2) + Mathf.Pow(target.position.z-myTransform.position.z,2) );
		//Debug.Log(distance);
		
		//Mäter om den är tillräckligt nära för att "slå" dig
		if(distance < 1){
			Debug.Log("Fuck you, coptard");
			// Kör DamageFunction i "HealthBarScript"
			HealthBarScript healthbarscript = GetComponent<HealthBarScript>();
			healthbarscript.DamageFunction(5);
			
		}
	}

}

Thanks in advance! :slight_smile:

The problems are entirely in your enemy script. There are a few non-critical issues here.

Do you really want to be using FindWithTag every single frame? Wouldn’t it be better to do that just once in ‘Start’, (or at least only when target == null), since there should only ever be one player? FindWithTag is pretty slow, and you want to avoid using it when you don’t have to.

As for the actual problem, it comes from this line-

HealthBarScript healthbarscript = GetComponent<HealthBarScript>();

The problem there is that it is trying to find a healthbar on the same object as the EnemyScript! (which obviously doesn’t work, because there isn’t one.) You should instead use

HealthBarScript healthbarscript = target.GetComponent<HealthBarScript>();

Which will return the correct component on the player, not the enemy.