-1

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

hugohugge
  • 1
  • 2

1 Answers1

-1

Find the answer yourself, it is not hard!

The error is in Detect_Blue.cs at line 84, which is this:

Debug.Log("Damaged: " + hitColliders[i].GetComponent<Detect_Red>().hitpoints.GetHealth());

If the error shows every time, you can do this: Put a breakpoint to that line, run the code and when it breaks there, put your mouse over particular expressions (including Debug, hitColliders[i] etc.) to see which one is null. (You need Visual Studio or similar debugger which evaluates expressions.)

If the error doesn't show at each run, you can put something like if (something == null) {;} and put break inside the if. Or you can also go the opposite way - if it crashes only in case of logging and the rest of program is OK, just use ?. instead of . and you will be safe - it will evaluate to null without exceptions. I don't know what logging system you use, but it should be safe with null as parameter.

Al Kepp
  • 5,831
  • 2
  • 28
  • 48