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

Im making a spawner need help!!

Discussion in 'Scripting' started by S3Critsss, Jan 12, 2020.

  1. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    So im making a simple game that has a spawner. here is my script. and the problem is that everytime the game runs it spawns the same enemy but i need it to spawn different enemies.
    the other problem is that everytime 2 enemies spawn it reloads the game. i need helpp
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Spaawner : MonoBehaviour
    7. {
    8.     public float startTimeBtwSpawn;
    9.     private float timeBtwSpawn;
    10.     public GameObject[] enemies;
    11.  
    12.     private void Update()
    13.     {
    14.         if (timeBtwSpawn <= 0) {
    15.             int rand = Random.Range(0, enemies.Length);
    16.             Instantiate(enemies[rand], transform.position,  Quaternion.identity);
    17.             timeBtwSpawn = startTimeBtwSpawn;
    18.         }else {
    19.             timeBtwSpawn -= Time.deltaTime;
    20.         }
    21.     }
    22. }
    23.  
    24.  
    and this is my enemy script
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class Enemy : MonoBehaviour
    8. {
    9.     public float speed;
    10.  
    11.     private void Update()
    12.     {
    13.         transform.Translate(Vector2.left * speed * Time.deltaTime);
    14.     }
    15.  
    16.     void OnTriggerEnter2D(Collider2D other)
    17.     {  
    18.  
    19.         if (other.CompareTag("Player")) {
    20.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    21.         }
    22.  
    23.         if (other.tag == gameObject.tag)
    24.         {
    25.             Destroy(other.gameObject);
    26.             Destroy(gameObject);
    27.         }else
    28.         {
    29.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    30.         }
    31.     }
    32. }
    33.  
    34.  
     
    Last edited: Jan 12, 2020
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,594
    Use
    Code (CSharp):
    1. Debug.Log ( "My message" ) ;
    To track errors and issues.

    What is the length of enemies.Length?

    In the second script, you reload scene, when either triggered object has Player tag, or no matching tag at all.
    So if second object is spawned, and collides with first, I presume it compares tag with each other. As the result, reloading a scene.
     
  3. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    The problem is that when an enemy that isnt a circle spawns it reloads the scene and my three enemies are circle square and triangle
     
    Last edited: Jan 12, 2020
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,594
    Sorry, I have no idea, what you mean by "circle spawn".
    Perhaps screenshot / drawing of the situation will be of any help?
    Still, please follow my initial suggestions and respond to questions I asked. Otherwise is difficult to even narrow the problem down.
     
  5. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    im talking about my enemies ones a square, ones a triangle, and ones a circle,
    when an enemy that isnt a circle spawns it destroys them or reloads the scene
    upload_2020-1-12_14-52-17.png
     
  6. VSM2018

    VSM2018

    Joined:
    Jun 14, 2018
    Posts:
    16
    I understand that there are 3 enemies circle, triangle and square. Like Antypodish pointed out there are two occasions when the scene gets reloaded: 1. When enemy collides with a Gameobject tagged as Player. 2. When enemy collides with a Gameobject which isn't tagged as enemy or isn't tagged at all. I suggest deleting that ELSE statement, since the scene should probably get reloaded only when the player gets hit.
    Also when all of your enemies have the enemy script, then you don't need Destroy(other.gameobject) under the enemy tag comparison statement. So make sure your enemies and player are tagged correctly.
    The enemies that spawn should be random, but this also means that one may get spawned many times in a row. Make sure your array is filled up correctly in the inspector and that there are all the different enemy prefabs you want to spawn.
     
    Last edited: Jan 12, 2020
  7. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    the square and triangle stay back but the cricle moves forward thats the problem not that the enemies collide with eachother and also i need the else statement upload_2020-1-12_15-39-58.png
     
  8. VSM2018

    VSM2018

    Joined:
    Jun 14, 2018
    Posts:
    16
    Do you have enemies script attached to the square and triangle enemies?