# “Instantiate” function in Unity spawns enemy but it doesn't show up

So, I'm making a top down shooter (2D) and I already made an enemy prefab. It walks to the player and damages it, and reacts to bullets physically until it eventually dies. The prefab itself is ok, because I can spawn it manually and it works as intended. The problem comes when I want to automate the spawn process with a script. So I made a script to do so, here it is:

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

public class GameController : MonoBehaviour
{

public GameObject enemy;

private float spawnPositionX;
private float spawnPositionY;

Vector2 spawnVector;

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

void SpawnEnemy()
{
spawnPositionX = Random.Range(-630f, 630f);
spawnPositionY = Random.Range(-340f, 340f);

spawnVector = new Vector2(spawnPositionX, spawnPositionY);

Instantiate(enemy, spawnVector, Quaternion.identity);
}
}


As you can see, it's nothing too complex. I just refer to a game object (which I linked by the way) and then create some variables that will hold the x and y positions in which the enemy will spawn at random. Then when I call the function, those positions get set and stored into a bi-dimensional vector to then be used with the instantiate function.

This... works... kinda? When I run the game, the instance of the enemy gets made, I can see the clone in the hierarchy and I can select it and see that it's indeed moving, but it's like... not there? I can't actually see it on the screen and it's not invisible as I can't interact with it either. It's really weird and I have no idea what's going on.

I tried to put the script on the camera, on the canvas, on a new empty object, nothing. I also checked that the prefab and the instance that's being spawned are indeed active. I thought about the coordinates being out of bounds, but when I check my player coordinates they are inbounds, so that can't be it either. I'm completely lost on this one, thoughts? Thanks.

• Looks like you're using a canvas for your gameplay content, but spawning your enemy at some random place in the world instead of within the canvas hierarchy. So it ends up existing in a separate coordinate system from your canvas objects, so even when it appears to overlap in the rendered view, it's not really in the same place and so doesn't interact. Zoom way out in the scene view and you'll probably find it's in a very different position than your other game content. – DMGregory Feb 4 at 1:08
• @DMGregory Interesting, I didn't know there were multiple coordinate systems. So how do you suggest I could solve this? I'm positive now this must be it, so how can I make sure everything is in the same coordinate system? – user128734 Feb 4 at 4:45
• Just adding that 600 units means 600 meters typically. So you’re spawning it in an area over one square kilometre in game! – Adam B Feb 4 at 7:11
• I'd need to see more of how the rest of your game is set up. Where do you manually put this prefab when not spawning it dynamically? – DMGregory Feb 4 at 12:32