Spawning

Hi there are a couple problems with my game right now I want to spawn enemies at the far right position on the green tile continuously, currently only certain enemies spawn where I want them to be bot not continuously and most of them lose there scale and spawn at the center the YouTube video below might help I also posted the code below to see if you all can help me thank you. (I’m also sorta new to game development) :slight_smile:

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

public class SecondarySpawnscript : MonoBehaviour
{
    public GameObject enemy;
    public GameObject ground;
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(Secondarycoroutine());
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    IEnumerator Secondarycoroutine()
    {
        float secondarynum = Random.Range(0.5f, 2);
        while (true)
        {
            yield return new WaitForSeconds(secondarynum);
            Instantiate(enemy, new Vector3(ground.transform.position.x + 5, 1, 0), enemy.transform.rotation);
        }
       

    }
}

the spawn manager script is here

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text;
using System;
using Random = UnityEngine.Random;

public class SpawnManager : MonoBehaviour
{
    // Variables
    public GameObject plane;
    public GameObject bad;

    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(SpawnThingy());
    }

    // Update is called once per frame
    void Update()
    {
    }

    IEnumerator SpawnThingy()
    {

        while (true)
        {
            int num = Random.Range(1, 3);
            if (num == 1)
            {
                yield return new WaitForSeconds(2);
                Instantiate(plane, new Vector3(0, 0, 15), plane.transform.rotation);
            }
            else if (num == 2)
            {
                yield return new WaitForSeconds(2);
                Instantiate(bad, new Vector3(0, 0, 15), bad.transform.rotation);
            }

        }
    }
}

and the enemy movement is here

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

public class dinosaurMovement : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(Vector3.left * Time.deltaTime * Random.Range(5, 20));
    }
}

thank you :slight_smile:

The second argument given to your various Instantiate() calls is the position where the object is created.

In 2 of those calls it is constant, and in one of those calls it is based on a ground position.

Because you write “thingy” and “enemy” and “bad” and “plane” it is kinda hard to even reason about what might be going on.

You might want to pause and go name things precisely what they are, call them dinosaurs or boxes or whatever, so you can reason about the entire space.

Beyond that, to help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

alright so I did what you did I renamed most of my code I also added some comments and some debug.log in there as well now I got it figured out however I can’t seem to think of a spawn position for my enemy I want my enemy to spawn at around the same location as the tile every 0.5 - 2 seconds the tile moves so I want the spawn location to move as well could you help me out with that (I only know grade 9 math)

this is the secondary spawn manager

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

public class SecondarySpawnscript : MonoBehaviour
{
    // Variables
    public GameObject enemy;
    private float zPos;

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Started coroutine");
        Debug.Log(Time.deltaTime);
        StartCoroutine(Secondarycoroutine());
        zPos = 20f;
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    // Spawns enemies at the plane with enemy ground tile
    IEnumerator Secondarycoroutine()
    {
        // Variables
        float randomNumber = Random.Range(0.5f, 2);
        Debug.Log(randomNumber);

        Debug.Log("Started loop");
        while (true)
        {
            yield return new WaitForSeconds(randomNumber);
            Debug.Log("Finished Delay");
            Vector3 spawnPos = new Vector3(25, 1, zPos);
            Debug.Log("Created position");
            // Spawn position needed here: Instantiate(enemy, spawnPos + new Vector3(0, 0, -5) * Time.deltaTime * 2, enemy.transform.rotation);
            Debug.Log("Spawned enemy");

        }
       

    }
}

this is the how the tiles move (I want to make it look like the camera is moving but the tiles are moving instead)

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

public class CameraMovement : MonoBehaviour
{
    // Variables
    private int entitys;

    // Start is called before the first frame update
    void Start()
    {
        entitys = 8;
    }

    // Update is called once per frame
    void Update()
    {
        if (entitys <= 10)
        {
            transform.Translate(Vector3.back * Time.deltaTime * 2);
        }
       
        if (transform.position.z <= -17.5f)
        {
            Destroy(gameObject);
        }
    }
}

also that suggestion you made really helped I’ll try to make that a habit thx :slight_smile: