I can't use a public non-static int in another script

I have this enemy and it has 2 scripts. One tells if it’s been hit and the other one basically does something if it has been hit. Now, I need to use an integer for the enemy’s health “wasphealth” in the Hit script but it only lets me do it if it’s static. I can’t really make it static because I’ll be duplicating this enemy. SO I need to implement a Singleton, right? Yeah, well for the life of me I couldn’t figure out how to do it… Do you guys have any suggestions?
Hit:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class hit : MonoBehaviour {

	
	public Rigidbody expl;

	void Start () {
		
	}
	void Update () {
	
	}

	void OnTriggerEnter (Collider other)
	{
		if (other.tag == "bullet") {
			colliderinstantiator.wasphealth += 1;
			Debug.Log (colliderinstantiator.wasphealth);

			Instantiate(expl, GameObject.FindWithTag("bullet").transform.position, Quaternion.identity);
			Destroy (GameObject.FindWithTag ("bullet"));
		}
	}
}

The other script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class colliderinstantiator : MonoBehaviour {

	public Rigidbody wasp;
	public Rigidbody pigeon;
	public Rigidbody bullet;
	public int wasphealth = 0; //this is the problem
	public float minDis = 10.0f;
	public float drag = 30.0f;
	public ParticleSystem smoke;
	public ParticleSystem broke;
	public Rigidbody eexpl;
	public bool smokerb = false;
	public bool brokerb = false;

	void Start () 
	{
		
	}
	void Update ()
	{
		if (wasp) {	
			if (wasp.transform.position.z - pigeon.transform.position.z < 200) {
				GetComponent<chabos>().enabled = false;
			}
			

			if (wasphealth >= 10 && wasphealth <= 20)
			{
				if (smokerb == false)
				{
					Invoke("Smoker", 0f);

				}
			}
			else if (wasphealth > 20) { Destroy(GameObject.FindWithTag("smoke1")); }
			
			if (wasphealth >= 21 && wasphealth <= 29) 
			{
				if (brokerb == false)
				{
					Invoke("Broker", 0f);
				}
			
			}
			else if (wasphealth > 29) { Destroy(GameObject.FindWithTag("broke1")); }
			if (wasphealth >= 30) {
				Destroy (GameObject.FindWithTag ("nmy1"), 0.1f);
				
			}
			if (wasphealth == 30) {
				Instantiate(eexpl, GameObject.FindWithTag("nmy1").transform.position, Quaternion.identity);

			}

		}
	}
	void Smoker ()
	{
		Instantiate (smoke, GameObject.FindWithTag ("nmy1").transform.position, Quaternion.identity);
		smokerb = true;
	}
	void Broker ()
	{
		Instantiate (broke, GameObject.FindWithTag ("nmy1").transform.position, Quaternion.identity);
		brokerb = true;
	}
}

NOTE: you don’t have to look at the whole script, just the int part

Implementing a singleton would just shift the problem in this case. You would end up only being able to have one enemy in the scene, which kinda defeats the purpose of all the OOP stuff you’re doing in these scripts. Instead, all you have to do is access the instance of the class, rather than the class itself;

void OnTriggerEnter (Collider other)
{
    colliderinstantiator instance = GetComponent<colliderinstantiator>();
    if (other.tag == "bullet") {
        instance.wasphealth += 1;
        Debug.Log (instance.wasphealth);
 
        Instantiate(expl, GameObject.FindWithTag("bullet").transform.position, Quaternion.identity);
        Destroy (GameObject.FindWithTag ("bullet"));
    }
}

A few things to note however;

  • There’s no reason these need to be separate scripts, just move your ‘hit’ code inside the main script
  • Not necessary, but it’s helpful to properly capitalise your class names to distinguish them from their instances
  • ‘Invoke (“”, 0f)’ is essentially the same as just calling the function, so it would be more beneficial to just do that, i.e. ‘Smoker()’
  • ‘FindWithTag()’ is a surprisingly expensive function, so if you can’t just assign the GameObject some other way, it would be better to only call it once and cache the result
    Hope that helps.