admin管理员组

文章数量:1395303

I want to multiply my prefab (bandit) every few seconds, but if I remove my prefab in the hierarchy it is no longer spawned. I put this in my project view as prefab but this doesn't seem to work. The scripts have been added to both bandit. But in the prefab of bandit, I can't do transform.player either. How do I solve this?

  • When the bandit of the hierarchy spawns, they always double instead of adding 1 every few seconds.
public class Enemy : MonoBehaviour
{
    public Transform player;  // Referentie naar de speler
    public float speed = 3f;  // Vijand
    public GameObject bandit;
    private float spawnTimer = 10f; // 30 sec
    private float timer = 0f; // houdt tijd bij

    void Update()
    {
        // Berekening richting speler
        Vector3 direction = (player.position - transform.position).normalized;

        // Vijand beweegt naar speler
        transform.position += direction * speed * Time.deltaTime;

        // Vijand kijkt naar speler
        transform.LookAt(player);

        timer += Time.deltaTime;

        if (timer >= spawnTimer)
        {
            // random positie tussen x = 0 en x = 119.1, y = -0.001 en z = 0 en 99.3
            Vector3 randomPos = new Vector3(Random.Range(0f, 119.1f), -0.001f, Random.Range(0f, 99.3f));

            Instantiate(bandit, randomPos, Quaternion.identity); // object wordt gespawnt zonder rotatie
            timer = 0f;
        }
    }
}

I want to multiply my prefab (bandit) every few seconds, but if I remove my prefab in the hierarchy it is no longer spawned. I put this in my project view as prefab but this doesn't seem to work. The scripts have been added to both bandit. But in the prefab of bandit, I can't do transform.player either. How do I solve this?

  • When the bandit of the hierarchy spawns, they always double instead of adding 1 every few seconds.
public class Enemy : MonoBehaviour
{
    public Transform player;  // Referentie naar de speler
    public float speed = 3f;  // Vijand
    public GameObject bandit;
    private float spawnTimer = 10f; // 30 sec
    private float timer = 0f; // houdt tijd bij

    void Update()
    {
        // Berekening richting speler
        Vector3 direction = (player.position - transform.position).normalized;

        // Vijand beweegt naar speler
        transform.position += direction * speed * Time.deltaTime;

        // Vijand kijkt naar speler
        transform.LookAt(player);

        timer += Time.deltaTime;

        if (timer >= spawnTimer)
        {
            // random positie tussen x = 0 en x = 119.1, y = -0.001 en z = 0 en 99.3
            Vector3 randomPos = new Vector3(Random.Range(0f, 119.1f), -0.001f, Random.Range(0f, 99.3f));

            Instantiate(bandit, randomPos, Quaternion.identity); // object wordt gespawnt zonder rotatie
            timer = 0f;
        }
    }
}
Share Improve this question edited Mar 16 at 21:23 derHugo 91.3k9 gold badges91 silver badges135 bronze badges asked Mar 15 at 15:46 Romy LambrechtsRomy Lambrechts 111 bronze badge 2
  • well anything that is instantiated (excluding dots for this) is in your heirachy, so you can find and remove it, or, better yet, keep track of it when you make it.. as for spawning 1 by 1, if you're getting 2 firstly, set the timer to zero first, secondly look for if this is in your scene more than once.. as unless your bandit is 2 items, this will spawn 1. Also look into object pooling – BugFinder Commented Mar 15 at 16:25
  • Sounds like you were better going with a central manager that handles the spawning rather than the bandit cloning itself .. – derHugo Commented Mar 16 at 21:25
Add a comment  | 

2 Answers 2

Reset to default 1

The design seems to be wrong. The spawning should've been done by another script, normally called a GameManager by most people.

Add an empty gameobject to the Scene, call it GameManager. Attach a GameManager script to it.
Inside the GameManager, handle the spawning. Additionally, I'd also prefer using a Coroutine instead of a timer:

public class GameManager : MonoBehaviour
{
    public GameObject bandit;

    public float spawnTimer = 30f;
    
    // you can also use the timer
    // but they seem to be a tacky approach imo
    void Start()
    {
        StartCoroutine(SpawnCoroutine()); // start the coroutine 

本文标签: