A function getting Called 100 times instead of just once, Help please (Solved)

I have a function that gets called with Right mouse Click, it suppose to get called once per unit that has this script, but for me its getting called approximately 100x per script.

Thanks in advance

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

public class ClickToMove : MonoBehaviour
{
    private Animator mAnimator;
    private NavMeshAgent mNavMeshAgent ;

    private bool mRunning = false;
    float count = 1;


    void Start()
    {
        mAnimator = GetComponent<Animator>();
        mNavMeshAgent = GetComponent<NavMeshAgent>();
       
    }

  
    void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        RaycastHit hit;

        if(Input.GetMouseButtonDown(1))
        {
            CalculateStoppingDistance();

            if (Physics.Raycast(ray, out hit, Mathf.Infinity))
            {
                if (mNavMeshAgent.tag == "Selected")
                {
                    mNavMeshAgent.destination = hit.point;
                }
               
            }
        }      

        if (mNavMeshAgent.remainingDistance <= mNavMeshAgent.stoppingDistance)
        {
            mRunning = false;
        }
        else
        {
            mRunning = true;
        }

        mAnimator.SetBool("Running", mRunning);
    }

    void CalculateStoppingDistance()
    {
        foreach (Unit myUnit in SelectionManager.SelectedUnitList)
        {
            if (mNavMeshAgent.tag == "Selected")
            {
                count = SelectionManager.SelectedUnitList.Count;
                mNavMeshAgent.stoppingDistance = count ;
                                                                     
                Debug.Log("count = " + count);
                Debug.Log("Stopping Distance = " + mNavMeshAgent.stoppingDistance);
            }
           
        }
    }
}

Well, do you have 100 units with this script attached? If so then you’ll call CalculateStoppingDistance() 100 times; once for each instance of this script in the scene.

I don’t understand why you’re looping through all Unit instances in SelectionManager.SelectedUnitList, but never even using myUnit. Doesn’t make sense.

1 Like

I have 98 units attached to it hence “count = 98”, I know my script isn’t efficient, but why does it call 100 times per script? why not once per script, sorry noob here.

wait, I think I get it, let me double check lol

“SelectionManager” isn’t provided so i’m not sure what the SelectedUnitList is, though i’ll suppose it’s just a List.

From what i can see, it’s not getting called 100 times, it’s getting called once for every GameObject this script it attached to. The “count” variable simply gets assigned a value of how many members there are in the List. At the time of debugging, the amount of members inside of the List is obviously 98, so we get 98 for every GameObject that calls this script.

1 Like

Love you bro, fixed lol

1 Like

yup, thx, I am stupid, I have a habit of calling the List every time I want to deal with it lol