Trying to make my Sphere's damage eachother but i get an error
tried to make a lot of things public in order for the scripts to be able to acess eachother
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Detect_Blue : MonoBehaviour
{
public Hitpoints hitpoints = new Hitpoints(100);
bool onetime = false;
bool times = false;
bool stop = true;
public float radius;
public Vector3 direction = new Vector3(2, 2, 2);
public void Start()
{
Debug.Log("Health: " + hitpoints.GetHealth());
InvokeRepeating("DetectEnemy", 4f, 2f);
}
public void Update()
{
if (hitpoints.GetHealth() == 0)
{
Destroy(gameObject);
}
}
public void DetectEnemy()
{
var hitColliders = Physics.OverlapSphere(direction, radius);
for (var i = 0; i < hitColliders.Length; i++)
{
if (hitColliders[i].CompareTag("Player"))
{
if (!onetime)
{
onetime = true;
print(hitColliders[i].tag);
InvokeRepeating("Hello", 1, 1);
}
}
}
}
public void Hello()
{
var hitColliders = Physics.OverlapSphere(direction, radius);
for (var i = 0; i < hitColliders.Length; i++)
{
if (hitColliders[i].CompareTag("Player"))
{
if (!times)
{
Collider coll = hitColliders[0];
Debug.Log("NameOfCollider" + coll.tag);
StartCoroutine(Damaged(coll));
times = true;
}
}
Debug.Log("Damaged: " + hitColliders[i].GetComponent<Detect_Red>().hitpoints.GetHealth());
if (hitpoints.GetHealth() == 0)
{
Destroy(gameObject);
}
if (hitColliders[i].CompareTag("Player"))
{
//hitColliders[i].GetComponent<Detect_Red>().hitpoints.Damage(10);
// Debug.Log("Damaged: " + hitColliders[i].GetComponent<Detect_Red>().hitpoints.GetHealth());
}
}
}
IEnumerator Damaged(Collider coll)
{
while (stop == true)
{
Debug.Log("NameOfCollider2" + coll.tag);
coll.GetComponent<Detect_Red>().hitpoints.Damage(10);
Debug.Log("Damaged: " + coll.GetComponent<Detect_Red>().hitpoints.GetHealth());
yield return new WaitForSeconds(1);
if (coll.GetComponent<Detect_Red>().hitpoints.GetHealth() == 0)
{
stop = false;
StopCoroutine(Damaged(coll));
Debug.Log("Healthhealth: " + coll.GetComponent<Detect_Red>().hitpoints.GetHealth());
Debug.Log("Courututu stopped");
stop = false;
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Detect_Red : MonoBehaviour
{
public Hitpoints hitpoints = new Hitpoints(100);
bool onetime = false;
bool times = false;
bool stop = true;
public float radius;
public Vector3 direction = new Vector3(2, 2, 2);
public void Start()
{
Debug.Log("Health: " + hitpoints.GetHealth());
InvokeRepeating("DetectEnemy", 4f, 2f);
}
public void Update()
{
if (hitpoints.GetHealth() == 0)
{
Destroy(gameObject);
}
}
public void DetectEnemy()
{
var hitColliders = Physics.OverlapSphere(direction, radius);
for (var i = 0; i < hitColliders.Length; i++)
{
if (hitColliders[i].CompareTag("Player"))
{
if (!onetime)
{
onetime = true;
print(hitColliders[i].tag);
InvokeRepeating("Hello", 1, 1);
}
}
}
}
public void Hello()
{
var hitColliders = Physics.OverlapSphere(direction, radius);
for (var i = 0; i < hitColliders.Length; i++)
{
if (hitColliders[i].CompareTag("Player"))
{
if (!times)
{
Collider coll = hitColliders[0];
Debug.Log("NameOfCollider" + coll.tag);
StartCoroutine(Damaged(coll));
times = true;
}
}
Debug.Log("Damaged: " + hitColliders[i].GetComponent<Detect_Blue>().hitpoints.GetHealth());
if (hitpoints.GetHealth() == 0)
{
Destroy(gameObject);
}
if (hitColliders[i].CompareTag("Player"))
{
//hitColliders[i].GetComponent<Detect_Blue> ().hitpoints.Damage(10);
// Debug.Log("Damaged: " + hitColliders[i].GetC`enter code here`omponent<Detect_Blue>().hitpoints.GetHealth());
}
}
}
IEnumerator Damaged(Collider coll)
{
while (stop == true)
{
Debug.Log("NameOfCollider2" + coll.tag);
coll.GetComponent<Detect_Blue>().hitpoints.Damage(10);
Debug.Log("Damaged: " + coll.GetComponent<Detect_Blue>().hitpoints.GetHealth());
yield return new WaitForSeconds(1);
if (coll.GetComponent<Detect_Blue>().hitpoints.GetHealth() == 0)
{
stop = false;
StopCoroutine(Damaged(coll));
Debug.Log("Healthhealth: " + coll.GetComponent<Detect_Blue>().hitpoints.GetHealth());
Debug.Log("Courututu stopped");
stop = false;
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Hitpoints
{
private int health;
public int healthMax;
public Hitpoints(int health)
{
this.health = health;
}
public int GetHealth()
{
return health;
}
public float GetHealthPercent()
{
return (float)health / healthMax;
}
public void Damage(int damageAmount)
{
health -= damageAmount;
if (health < 0)
{
health = 0;
}
}
public void Heal(int healAmount)
{
health += healAmount;
if (health > healthMax)
{
health = healthMax;
}
}
}
NullReferenceException: Object reference not set to an instance of an object Detect_Blue+c__Iterator0.MoveNext () (at Assets/Detect_Blue.cs:108) UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) Detect_Blue:Hello() (at Assets/Detect_Blue.cs:77)
NullReferenceException: Object reference not set to an instance of an object Detect_Blue.Hello () (at Assets/Detect_Blue.cs:84)
I want it to be able to lower the other ones health