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

Extra GameObject are created

Discussion in 'Scripting' started by Zavalichi, Oct 23, 2018.

  1. Zavalichi

    Zavalichi

    Joined:
    Oct 13, 2018
    Posts:
    162
    Hi guys,

    I have two scripts: CarsSpawner and SpawnHere
    CarsSpawner generate Game Objects (SpawnPlace) where I can generate cars at every 1second.
    SpawnHere is the component for every SpawnPlace where the cars are generated.

    If I have "x" SpawnPlace the script creates for me "x" Game Objects ( OK )
    If I have "y"-cars for every SpawnPlace the script creates "y" cars and "x * y" Spawn Place ( NOK )

    Here is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class SpawnHere: MonoBehaviour
    5. {
    6.     // Numărul maxim de mașini care pot fi generate
    7.     [HideInInspector]
    8.     public int maxCar;
    9.  
    10.     // Obiectele folosite pe post de mașini
    11.     [HideInInspector]
    12.     public GameObject b1;
    13.     [HideInInspector]
    14.     public GameObject b2;
    15.     [HideInInspector]
    16.     public GameObject c1;
    17.     [HideInInspector]
    18.     public GameObject c2;
    19.     [HideInInspector]
    20.     public GameObject c3;
    21.     [HideInInspector]
    22.     public GameObject c4;
    23.     [HideInInspector]
    24.     public GameObject t;
    25.     [HideInInspector]
    26.     public GameObject p;
    27.     [HideInInspector]
    28.     private GameObject go;
    29.  
    30.     void Start()
    31.     {
    32.         StartCoroutine(SpawnCar());
    33.     }
    34.     public void selectRandomCar(out GameObject go)
    35.     {
    36.         int car = UnityEngine.Random.Range(1, 7);
    37.         go = null;
    38.  
    39.         switch (car)
    40.         {
    41.             case 1:
    42.                 {
    43.                     go = b1;
    44.                     go.name = "Bus_1";
    45.                     break;
    46.                 }
    47.             case 2:
    48.                 {
    49.                     go = b2;
    50.                     go.name = "Bus_2";
    51.                     break;
    52.                 }
    53.             case 3:
    54.                 {
    55.                     go = c1;
    56.                     go.name = "Car_1";
    57.                     break;
    58.                 }
    59.             case 4:
    60.                 {
    61.                     go = c2;
    62.                     go.name = "Car_2";
    63.                     break;
    64.                 }
    65.             case 5:
    66.                 {
    67.                     go = c3;
    68.                     go.name = "Car_3";
    69.                     break;
    70.                 }
    71.             case 6:
    72.                 {
    73.                     go = c4;
    74.                     go.name = "Car_4";
    75.                     break;
    76.                 }
    77.             case 7:
    78.                 {
    79.                     go = t;
    80.                     go.name = "Taxi";
    81.                     break;
    82.                 }
    83.             default:
    84.                 break;
    85.         }
    86.     }
    87.  
    88.     IEnumerator SpawnCar()
    89.     {
    90.         int i = 0;
    91.         while (maxCar > 0)
    92.         {
    93.  
    94.             maxCar--;
    95.             Vector3 stayOnSide = Vector3.zero;
    96.  
    97.             //Get random cars
    98.             Vector3 aux = Vector3.zero;
    99.  
    100.            
    101.             selectRandomCar(out go);
    102.             go = Instantiate(go, gameObject.transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
    103.             go.AddComponent<CarManager>();
    104.  
    105.             yield return new WaitForSeconds(1);
    106.  
    107.         }            
    108.     }
    109. }
    110.  
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. class CarsSpawner : MonoBehaviour
    5. {
    6.     // Numărul maxim de mașini care pot fi generate
    7.     public int maxCar;
    8.     public GameObject b1;
    9.     public GameObject b2;
    10.     public GameObject c1;
    11.     public GameObject c2;
    12.     public GameObject c3;
    13.     public GameObject c4;
    14.     public GameObject t;
    15.     public GameObject p;
    16.     void Start()
    17.     {
    18.         List<Node> spawnerPlace;
    19.         SpawnerPlace(out spawnerPlace);
    20.        
    21.  
    22.         for (int i = 0; i< spawnerPlace.Count; ++i)
    23.         {
    24.             GameObject spawner = new GameObject();
    25.             spawner.name = "Spawner Place: " + i.ToString();
    26.             spawner.transform.position = spawnerPlace[i].Point;
    27.             spawner.AddComponent<SpawnHere>();
    28.            
    29.             spawner.GetComponent<SpawnHere>().b1 = b1;
    30.             spawner.GetComponent<SpawnHere>().b2 = b2;
    31.             spawner.GetComponent<SpawnHere>().c1 = c1;
    32.             spawner.GetComponent<SpawnHere>().c2 = c2;
    33.             spawner.GetComponent<SpawnHere>().c3 = c3;
    34.             spawner.GetComponent<SpawnHere>().c4 = c4;
    35.             spawner.GetComponent<SpawnHere>().t = t;
    36.             spawner.GetComponent<SpawnHere>().p = p;
    37.             spawner.GetComponent<SpawnHere>().maxCar = maxCar;
    38.         }
    39.  
    40.     }
    41.  
    42.     private void SpawnerPlace (out List<Node> cityBounds)
    43.     {
    44.         cityBounds = new List<Node>();
    45.         GameObject map = GameObject.Find("Map");
    46.         List<Node> graph = map.GetComponent<GlobalData>().Graph;
    47.  
    48.         // Adaug posibilele puncte care reprezintă marginile orașului
    49.         for( int i = 0; i < graph.Count; ++i)
    50.         {
    51.                 if(graph[i].connections.Count == 1)
    52.                 {
    53.                     cityBounds.Add(graph[i]);
    54.                 }
    55.         }
    56.  
    57.         // Verific dacă posibilele puncte se mai găsesc și în altă parte
    58.         for (int i = 0; i < graph.Count; ++i)
    59.         {
    60.             for (int j = 0; j < cityBounds.Count; ++j )
    61.             {
    62.                 if (graph[i].Connections.Count >0 )
    63.                 {
    64.                     if (graph[i].connections[0].ID == cityBounds[j].ID)
    65.                     {
    66.                         cityBounds.Remove(graph[i]);
    67.                     }
    68.                 }
    69.                
    70.             }
    71.            
    72.         }
    73.     }
    74.  
    75. }
    76.  
    77.  
    Can you help me?
    Thank you!
     

    Attached Files:

    • why.PNG
      why.PNG
      File size:
      714.3 KB
      Views:
      620