0
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Feb 4 at 1:08
  • \$\begingroup\$ @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? \$\endgroup\$ – user128734 Feb 4 at 4:45
  • \$\begingroup\$ Just adding that 600 units means 600 meters typically. So you’re spawning it in an area over one square kilometre in game! \$\endgroup\$ – Adam B Feb 4 at 7:11
  • \$\begingroup\$ 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? \$\endgroup\$ – DMGregory Feb 4 at 12:32
0
\$\begingroup\$

Using a canvas to display gameplay content probably isn’t a good idea, canvases are pretty inefficient and are designed mostly for UI display.

You probably want to look into the Sprite class and other 2d classes (assuming you’re game is 2d). There’s also some great tutorials on official unity learn website to get you started on 2D objects and spawning. Also, Jason Weimann has some great tutorials on YouTube for getting started on 2D games.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Select the spawned enemies in the Hierarchy, and then press F on your keyboard. Now if you check your Scene view, you should see what your enemies doing, where they are, etc.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.