Enemy Object Not Rendering

I’m attempting to make a Tower defense game following the tutorial below:

And the enemy game objects won’t render


However, on the left, the game object still spawn in a staggered fashion and exist in the game engine

Does anyone know why it wouldn’t be rendering

New Script

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class WaveSpawner : MonoBehaviour

{

public Transform enemyPrefab;

public Transform spawnPoint;

public float timeBetweenWaves = 5f;

private float countdown = 2f;

private int wave = 0;

void Update()

{

if(countdown <= 0f)

{

StartCoroutine(SpawnWave());

countdown = timeBetweenWaves;

}

countdown -= Time.deltaTime;

}

IEnumerator SpawnWave ()

{

wave++;

for (int i=0; i < wave ;i++) {

spawnEnemy();

yield return new WaitForSeconds(0.5f);

}

}

void spawnEnemy() {

Instantiate(enemyPrefab, spawnPoint.position, spawnPoint.rotation);

}

}

Scripts from before:

using UnityEngine;

public class Waypoints : MonoBehaviour

{

public static Transform[ ] points;

void Awake()

{

points = new Transform[transform.childCount];

for (int i = 0; i < points.Length; i++)

{

points *= transform.GetChild(i);*
*}*
*}*
*```*
*```*
*using System.Collections;*
*using System.Collections.Generic;*
*using UnityEngine;*
*public class Enemy : MonoBehaviour {*
*public float speed = 10f;*
*//Supposed to make enemy move to next waypoint*
*private Transform target;*
*private int wavepointIndex = 0;*
*// Start is called before the first frame update*
*void Start()*
*{*
*target = Waypoints.points[0];*
*}*
*// Update is called once per frame*
*void Update()*
*{*
*Vector3 dir = target.position - transform.position;*
_transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);_

*if (Vector3.Distance(target.position, transform.position)< 0.3f)*
*{*
*GetNextWaypoint();*
*}*

*}*
*void GetNextWaypoint() {*

*if (wavepointIndex >= Waypoints.points.Length - 1)*
*{*
*Destroy(gameObject);*
*return;*
*}*
*wavepointIndex++;*
*target = Waypoints.points[wavepointIndex];*

*}*
*}*
*```*
*For clarity, I use X-code to edit my scripts*

Steps to figure this out:

  • Are you seeing any runtime errors in the log while running? If so, fix them.

  • Put a Debug.Log() in where the spawn code is. Does that code even execute?

  • If that code executes, after the code executes press PAUSE in the editor and go find your enemy in the Hierarchy. Is it visible? Is it where you expect it to be?

If the above ideas didn’t work, check these ones:
-Does your gameObject has it’s MeshRenderer turned on?
-Does the mesh renderer has a mesh assigned to it?
-Does the mesh renderer has a material assigned to it?
-Check the material alpha and transparency.
-If the object is in a layer, check that the camera is rendering that layer (go to the camera component and check the culling mask, to see that the layer is ticked)
-If you exported the mesh from another app check that the normals are not inverted.
-Check that the object is between the far and near clipping planes.
-If you have a 2D plane for the board, check to see if your object is not rendering behind it: turn off the plane or whatever is in front to check for this. This can happen sometimes if your using a canvas.

Post images showing the mesh, and the inspector. Compare in scene view an game view.

Here’s the Game and hierarchy views:



This is actually your moment to shine since you have the operating game in your hands and ready to investigate in Unity.

Press PAUSE in the editor and start inspecting every element in your hierarchy, using the checklists I posted and @calpolican posted.

Both of those lists should give you a LOT of different things to check over, and probably will guide you to either a better understanding of the problem, or an actual solution.

I checked the enemies in the hierarchy and realized they weren’t moving from the spawn box, and actually rendering fine. This lead me to realize the enemy script hadn’t been attached to the enemy pre-fab, and the game works as intended when the enemies have their script

Thank you for your help!

1 Like

AWESOME! Congratulations. This is the beauty of Unity3D: something not behaving? Push pause and go look at it!

“Hey, what are you guys still doing in the spawnbox?!”

Also, if you want to press PAUSE at a precise moment in your game, like when some event happens, did you know you can just put this statement into your code and when it executes the Editor will enter pause mode.

Debug.Break();  // causes the Unity editor only to enter pause mode (suspend playing)

It does not affect your final game at all. It’s SUPER helpful if you think something is a little bit “off” and you can’t quite check it in realtime play easily, like a gun bullet is coming out a little bit “off center,” for instance.

1 Like