Enemy appears at bottom, left corner of screen

When I click play on my game the enemy spaceship appears in the bottom left corner instead of where I’ve placed the EnemyFormation game object in the top centre.

Any idea what might have gone wrong?

Without more information I can only stab in the dark; Do you feel like making a Pastebin of your script?

The GameObject prefab being instantiated: what is the transform set to? What object is the script in question attached to?

Is this enough to go on?
I feel like I copied Brice verbatim!

Apologies as always for being the eternal noob.

I found if I move the Main camera separately from the canvas it sort of solves the problem - although that seems like a clumsy work-around and is probably going to cause issues in the future?

using UnityEngine;
using System.Collections;

public class EnemySpawner : MonoBehaviour {
public GameObject enemyPrefab;

// Use this for initialization
void Start () {
	GameObject enemy = Instantiate(enemyPrefab, new Vector3 (0,0,0), Quaternion.identity) as GameObject;
	enemy.transform.parent = transform;
}
// Update is called once per frame
void Update () {

}

}

Ahh I think I see the issue…

void Start () {
	GameObject enemy = Instantiate(enemyPrefab, new Vector3 (0,0,0), Quaternion.identity) as GameObject;
	enemy.transform.parent = transform;
}

So to break it down, the instantiate command takes 3 parameters:

  1. What to create (in your case, enemyPrefab)
  2. Where to create it (You give it the world origin here: new Vector3(0,0,0))
  3. How to orient it (Quaternion.identity).

So in this case, it wouldn’t matter where in the world your EnemySpawner is, because it will instantiate the enemyPrefab at world-origin every time.

If you need more help, just ask, but I think this might be enough help. Cheers!

Thanks. From talking on here and with a guy I know on the same course on whatsapp I think I’ve worked out my confusion!

First up I had Main camera and canvas lined up on screen but in Canvas inspector I had it set to screen space - overlay not screen space - camera.

This made the enemy spaceship appear in centre of screen but I didn’t understand why I couldn’t make them appear at any other position - I now understand that EnemyFormation gameobject will always be at position 0,0,0 but the actual formation of enemies can be changed around it - currently represented by all the white circles.

This is what I get for trying to code at 1am!!!

Thanks for your help and patience

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms