Ram isn’t Spawning when i run the game

hello there, that my Ram isn’t Spawning when i run the game, here is my code →

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

public class ObjectPool : MonoBehaviour
    [SerializeField] GameObject enemyPrefab;
    [SerializeField] float spawnTimer = 1f;
    void Start()

    IEnumerator SpawnEnemy()
            Instantiate(enemyPrefab, transform);
            yield return new WaitForSeconds(spawnTimer);

The script is placed inside the ObjectPool GameObject and the Ram is also in the SerializeField, i can provide more detail if needed.

Hi verse,

When debugging, the first step is usually to check if your code gets executed. That’s impossible to tell just by reading the code. Please add a Debug.Log to the code block of the while-loop, then play your game to see if the message appears.

If it does not appear, check the Start method with a Debug.Log. If none of the methods get called, you know that the problem is very likely not in this code.

Thank you for this helpful Advice Nina!, i added the Debug Statements and it does print on both Start and the While Loop.

That’s great. Now we know that your code works. :slight_smile:

There are at least two potential reasons why it seems that the rams do not spawn:

  • The rams get spawned ‘somewhere else’ in the scene or they are invisible. Please check your Hierarchy. Do the enemies appear there?
  • The rams get spawned and destroyed immediately. Add a Debug.Log to the Start method of your Enemy script, to see if it gets called. If you expect to see 5 enemies, there should be 5 messages in your console.

nothing in my Hierarchy, i added a Debug.Log to start of my enemy script and its actually printing!

If the message of the Debug.Log you added to the enemy script appear in your console, this means that the enemies existed at least for a brief moment. Otherwise, the messages would not have appeared.

And this leaves us with one remaining scenario (given you don’t know any other reason why your enemies don’t appear): The enemies get destroyed a moment after they got created.

Now try to figure out what might destroy them. Add another Debug.Log to the method which destroys the respective enemy. If that message appears directly after the message from the Start() method, you found the problem.

The next step would be to look for the reason why the enemy gets destroyed. The enemy could destroy itself, or a projectile of a tower could destroy the enemy, or there is a script attached to the wrong game object, which destroys the enemy.

As you can hopefully see, we systematically narrow the problem down. It’s not difficult but it can be a bit annoying because you have to check the ‘simplest’ details. If you find something else suspicious while debugging, check that, too. If you are lucky, your suspicion is correct, and you find the problem faster. If not, it’s only a matter of time until you find it. :slight_smile:

1 Like

In the EnemyHealth script where theres a Destroy(gameObject); i added some Debug.Logs’s and non of them print after the enemy has been created, the Ram has the EnemyHealth & enemy script, Thank you for your guidance, i will begin investigating even further.

That’s odd. :confused:

Maybe try to add Unity’s OnDestroy method to your script. In that method, log a message into your console. If Unity destroys the object, you should see that message. The problem with the non-Unity methods is that they are not part of Unity’s lifecycle system, so it might be that they do not get called for some reason.

If the messages from the OnDestroy method appear, you know that your enemies existed and got destroyed. However, they did not get destroyed by your EnemyHealth class. Maybe another object destroys them for some reason. That can be a bit bothersome to debug because there is no direct way to figure out which object called the Destroy method to destroy the enemy.

Also use Debug.Log to see to which game objects your Enemy components are attached. You can do that by logging the name of the current game object into your console.

How are you getting on with this, @verse?

Privacy & Terms