Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Scripting' started by Mortalanimal, Oct 22, 2019.

  1. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    566
    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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class ClickToMove : MonoBehaviour
    7. {
    8.     private Animator mAnimator;
    9.     private NavMeshAgent mNavMeshAgent ;
    10.  
    11.     private bool mRunning = false;
    12.     float count = 1;
    13.  
    14.  
    15.     void Start()
    16.     {
    17.         mAnimator = GetComponent<Animator>();
    18.         mNavMeshAgent = GetComponent<NavMeshAgent>();
    19.        
    20.     }
    21.  
    22.  
    23.     void Update()
    24.     {
    25.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    26.  
    27.         RaycastHit hit;
    28.  
    29.         if(Input.GetMouseButtonDown(1))
    30.         {
    31.             CalculateStoppingDistance();
    32.  
    33.             if (Physics.Raycast(ray, out hit, Mathf.Infinity))
    34.             {
    35.                 if (mNavMeshAgent.tag == "Selected")
    36.                 {
    37.                     mNavMeshAgent.destination = hit.point;
    38.                 }
    39.                
    40.             }
    41.         }      
    42.  
    43.         if (mNavMeshAgent.remainingDistance <= mNavMeshAgent.stoppingDistance)
    44.         {
    45.             mRunning = false;
    46.         }
    47.         else
    48.         {
    49.             mRunning = true;
    50.         }
    51.  
    52.         mAnimator.SetBool("Running", mRunning);
    53.     }
    54.  
    55.     void CalculateStoppingDistance()
    56.     {
    57.         foreach (Unit myUnit in SelectionManager.SelectedUnitList)
    58.         {
    59.             if (mNavMeshAgent.tag == "Selected")
    60.             {
    61.                 count = SelectionManager.SelectedUnitList.Count;
    62.                 mNavMeshAgent.stoppingDistance = count ;
    63.                                                                      
    64.                 Debug.Log("count = " + count);
    65.                 Debug.Log("Stopping Distance = " + mNavMeshAgent.stoppingDistance);
    66.             }
    67.            
    68.         }
    69.     }
    70. }
    71.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
    Mortalanimal likes this.
  3. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    566
    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.
     
  4. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    566
    wait, I think I get it, let me double check lol
     
  5. crysicle

    crysicle

    Joined:
    Oct 24, 2018
    Posts:
    95
    "SelectionManager" isn't provided so i'm not sure what the SelectedUnitList is, though i'll suppose it's just a List<someting>.

    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<someting>. 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.
     
    Mortalanimal likes this.
  6. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    566

    Love you bro, fixed lol

     
    Joe-Censored likes this.
  7. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    566
    yup, thx, I am stupid, I have a habit of calling the List every time I want to deal with it lol