Objects instantiating at 0,0,0 occasionally (bug?)

On this scene, I only have 2 game objects. 1 empty object with a spawner script, and 1 with sprite renderers, colliders, and etc for the map.

The spawner script instantiates enemy ships on each spawn points at the start function.

Map:

My spawn points are those white circles outside the star.

What should happen:

As you can see, the enemy ships are on their corresponding position.

The enemy ships contains a sprite renderer, polygon collider, and a rigidbody.

What happens often:

At the bottom left, you see some enemy ships spawning at 0,0 for some reason I can’t figure out…

Spawn Script (simplified) (yeah, bug still occurs with this script):

    public SpawnPoints top;
    public GameObject t1Enemy;
    public float spawnRate;

    void Start () {
        StartCoroutine(Spawn());
    }

    IEnumerator Spawn () {
        while (true) {
            T1Spawn();

            yield return new WaitForSeconds(spawnRate);
        }
    }

     void T1Spawn () {
            Instantiate(t1Enemy, top.one.position, top.one.rotation);
            Instantiate(t1Enemy, top.two.position, top.two.rotation);
            Instantiate(t1Enemy, top.three.position, top.three.rotation);
            Instantiate(t1Enemy, top.four.position, top.four.rotation);
            Instantiate(t1Enemy, top.five.position, top.five.rotation);
            Instantiate(t1Enemy, top.six.position, top.six.rotation);
            Instantiate(t1Enemy, top.seven.position, top.seven.rotation);
            Instantiate(t1Enemy, top.eight.position, top.eight.rotation);
            Instantiate(t1Enemy, top.nine.position, top.nine.rotation);
      }

TL;DR: Game Objects are spawning at 0,0 occasionally even if I stated on the script to spawn them at the specific spawn points.
What am I doing wrong?

Are you changing or assigning the SpawnPoints from some other script?

Nope. It’s at the same script.

public class SpawnPoints {
    public Transform one, two, three, four, five, six, seven, eight, nine;
}

It’s where I put the spawn point’s transform.

Though this doesn’t help with the problem, but maybe you should consider using an array instead of nine separate variables.

Eh, I wouldn’t see the difference on this one and I’ll end up doing top.spawn[0] which looks terrible in my eyes.

On topic:
Forgot to mention that when this bug occurs, it drops my frame rate to 0 in little time to the point it sort of crashes Unity.

The benefit would be quite obvious, if you want to add more spawn points you can do so easily. It also makes the code much more readable.

public class SpawnPoints 
{
    public Transform points[];
}

public SpawnPoints top;
    public GameObject t1Enemy;
    public float spawnRate;

    void Start () {
        StartCoroutine(Spawn());
    }

    IEnumerator Spawn () {
        while (true) {
            T1Spawn();

            yield return new WaitForSeconds(spawnRate);
        }
    }

     void T1Spawn () {
        for(int i = 0; i < top.points.length; i++)
            Instantiate(t1Enemy, top.points[i].position, top.points[i].rotation);
     }
1 Like

Not only that, but it reduces copy & paste errors as well. But like I’ve said, it’s off-topic. I just meant to give a friendly suggestion.

1 Like

Wow! That’s actually what I was trying to do before but gave up on it due to the lack of knowledge. I should’ve asked what would it benefit me earlier instead of being confident that there’s no difference.

Thanks, Heuvel and blizzy!

Though I’m still figuring out how to fix this bug…

is it always the same ones that spawn in the wrong place? All correctly assigned in inspector?

Nope, it’s random.

Basically, if I load the scene, there will be a chance that this: http://puu.sh/jxqPH/009613a86c.png, will happen.

There’s also a chance that it doesn’t happen when I load the scene and this bug won’t occur over time.

Is there any behaviour on the enemies that may cause the to move

On this scene, no. I turned off my enemy ai script and this still occurs.

Also, on top of this bug, game objects that have rigidbody, when they move towards the wall, they teleport back to 0,0 including my player ship.

I was surprised because I don’t even have a teleporting script yet it teleports my character to 0,0 when I bump into the walls.

The walls have colliders and a rigidbody that has x, y, z contraints if this information helps.

Boop.

Still trying to fix the bug… zz

I’ve never tried to instantiate more than 2 objects simultaneously, but perhaps you could make the T1 spawn a coroutine as well and use “return yield null;” to skip a frame between instantiations. It’ll make each “set” take 9 frames, but if you instantiate them as disabled (or turn their renderer off) and then enable them all when the set is finished, you can make them appear at the same time if that’s a big deal to you.

I just want to see if this might be a problem with too many simultaneously instantiated objects. You should also put little renderers on your spawn points and see if maybe those are what’s moving, and not the instantiated objects ignoring them.

Are the spawn points placed in editor or during runtime?

I tried to create a countdown before I start the spawn coroutine and the bug still occured after 8 times of playing and un playing the scene.

I also tried spawning only one prefab and the bug still occured after 5 times of playing and un playing the scene.

The spawn points are placed in the editor, yes.

I FIXED IT, I HOPE!

My Enemy Ships Prefab’s rigidbody2d has constraints that freezes X and Z. I unchecked them and they don’t spawn in the middle anymore. But if I check them back, they spawn in the middle again.

I also tried turning on the constraints AFTER they spawn and they teleport back to 0,0.

Rookie mistake… Silly me!

Thank you all anyway for trying to help me!

2 Likes

Are you able to turn on the constraints in code? Would they still spawn in the middle?