Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

My method is adding new gameobjects in to the game... (Please help)

Discussion in 'Scripting' started by briyan, Aug 13, 2015.

  1. briyan

    briyan

    Joined:
    Jan 13, 2015
    Posts:
    52
    Hello, iam trying a new way of programming (with a lot of stack overflow as result). So i fixed the stack overflow but know is my script adding new gameobject's to the game. I know why it happens, but don't know how to prevent stack overflow if i remove that line...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class WhiteSoldierAI_1stEdition : MonoBehaviour {
    6.  
    7.     //this are all the bool's
    8.  
    9.     //this bool say's if the soldier is alive or not
    10.     private bool alive;
    11.     //this is if the game starts for the first time
    12.     private bool first_time_1;
    13.  
    14.     //this are all the gameobject's
    15.  
    16.  
    17.     //this are all the components
    18.  
    19.     //the navmeshagent component, this is for the pathfinding
    20.     public NavMeshAgent agent;
    21.  
    22.     //this are al the int's
    23.  
    24.     //this int is just testing if the closest works
    25.     public float closest_distance_test;
    26.  
    27.  
    28.     // Use this for initialization
    29.     void Start ()
    30.     {
    31.         //the soldier will alway's start alive in the game
    32.         alive = true;
    33.         //the game laucnhes for the first time
    34.         first_time_1 = true;
    35.     }
    36.  
    37.  
    38.     // Update is called once per frame
    39.     void Update ()
    40.     {
    41.         //this will be setup for the test
    42.         closest_distance_test = Vector3.Distance(transform.position, Closest ().transform.position);
    43.     }
    44.  
    45.     //this list contains all the enemies in the current level
    46.     private List<GameObject> enemies()
    47.     {
    48.         //i will add a new enemies list
    49.         List<GameObject> enemies_list = new List<GameObject>();
    50.         //if there are no enemies added
    51.         if(enemies().Count == null)
    52.         {
    53.         //now will i add al the enemies in the list
    54.         enemies_list.AddRange(GameObject.FindGameObjectsWithTag("Black_Soldier"));
    55.         }
    56.         //the enemies list will be returned after adding all the enemies
    57.         return(enemies_list);
    58.     }
    59.  
    60.     //this list contains all the distance's from the soldier to the enemy
    61.     private List<float> enemies_distances()
    62.     {
    63.         //i will add a new list
    64.         List<float> enemies_distances_list = new List<float>();
    65.         //now will i add al the float's
    66.         enemies_distances_list.AddRange(new float[enemies ().Count]);
    67.         //now will i open a loop
    68.         for(int i = 0; i<enemies_distances_list.Count;i++)
    69.         {
    70.             //now will i calculate the distance
    71.             enemies_distances_list[i] = Vector3.Distance(enemies ()[i].transform.position, transform.position);
    72.         }
    73.         //i will now return the list after all the distance's was calculated
    74.         return(enemies_distances_list);
    75.     }
    76.  
    77.     //here will the closest enemy be calculated
    78.     private GameObject Closest()
    79.     {
    80.         //we make a variable for the closeset distance
    81.         float closest_distance = new float();
    82.         //a closest gameobject
    83.         GameObject closest_inside = new GameObject();
    84.         //first we need to setup a loop
    85.         for(int i = 0; i < enemies_distances().Count; i++)
    86.         {
    87.             //then we calculate the closest enemy
    88.             if(closest_distance == 0 || enemies_distances()[i] <= closest_distance)
    89.             {
    90.                 //then we will set the closest_distance and the closest to the closest enemy and distance
    91.                 closest_distance = enemies_distances()[i];
    92.                 //then the closest will be set
    93.                 closest_inside = enemies ()[i];
    94.             }
    95.         }
    96.         //this is just for returning
    97.         return(closest_inside);
    98.     }
    99.  
    100. }
    101.  
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Going to be pretty honest here mate - this code is a complete mess. You're doing some really, really weird things that don't make a whole lot of sense.

    The source of your issue is probably this:
    Code (csharp):
    1.  
    2.  
    3.   private List<GameObject> enemies()
    4.   {
    5.        //i will add a new enemies list
    6.       List<GameObject> enemies_list = new List<GameObject>();
    7.       //if there are no enemies added
    8.       if(enemies().Count == null)
    9.       {
    10.             //now will i add al the enemies in the list
    11.             enemies_list.AddRange(GameObject.FindGameObjectsWithTag("Black_Soldier"));
    12.       }
    13.  
    14.        //the enemies list will be returned after adding all the enemies
    15.        return(enemies_list);
    16.  }
    17.  
    You're calling the function, from inside the function, here:
    Code (csharp):
    1.  
    2.       if(enemies().Count == null)
    3.  
    Are you sure you didn't mean to use enemies_list there?

    That's just scratching the surface of issues I see but that's where your stack overflow is coming from: You call the enemies() function, then inside that function you call enemies() which calls a new enemies() function which inside that function calls the enemies() function again... so on and so on infinitely.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. GameObject closest_inside =new GameObject();
    3.  
    "create a new game object and assign it to the variable"...

    Code (csharp):
    1.  
    2. GameObject closest_inside;
    3.  
    "create a variable of type GameObject"

    you want the second one.

    but you're doing it the hard way trying to maintain two lists, one of gameobject and one of distances. You can have a single list of the enemies and sort them by their properties

    i.e.

    Code (csharp):
    1.  
    2. using System.Linq;
    3.  
    4. // class level variable to hold enemies
    5. private List<GameObject> enemies;
    6.  
    7. // inititlise the enemies list when you need to
    8.  
    9.  
    10. private GameObject GetClosestEnemy()
    11. {
    12.     enemies.Sort(
    13.                     (unit1, unit2) =>
    14.                     (transform.position - unit1.transform.position).sqrMagnitude.CompareTo(
    15.                         (transform.position - unit2.transform.position).sqrMagnitude)
    16.                     );
    17.                    
    18.     return enemies.First();
    19. }
    20.  
    21.  
     
    briyan likes this.
  4. briyan

    briyan

    Joined:
    Jan 13, 2015
    Posts:
    52
    Well, i tried some new things out, but i would like to get some help with it because this way of programming is completly new for me...

    So it's hard to understand because i never used this functions before (the functions with variables). So what should i change?
     
  5. briyan

    briyan

    Joined:
    Jan 13, 2015
    Posts:
    52

    this look good men, wow hahah.

    Could you explain what this exactly do, because i used another methode for this.
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148