Game logic confusion...

Hi,

I’m just starting out learning Unity and am trying to make a basic Space Invaders engine (yawn I know, sorry!).

I’m getting a grasp of the language (using C#) slowly, but my question is more about where to put scripts etc.

For my game, I’m instantiating the enemy ships in a script I’ve attached to the main camera (good/bad idea?) and it looks like this;

for (var x=0; x<4; x++) {
 GameObject enemyShip = Instantiate(enemyPrefab, enemyPositions[x], Quaternion.identity) as GameObject;
 }

Just to get 4 enemies on the screen (the positons are taken from a Vector3 List as I asked yesterday).

This works fine. Now to move the enemies, would you do this within a script attached to the enemy prefab, or in the update of the GameManager script?

What my logic is, is to;

  1. Loop through all enemy objects using;

    var enemyCrafts = GameObject.FindGameObjectsWithTag(“enemy”);

  2. Using a foreach to then transform each object to the right. I then need to check all the objects x positon and if it is > than the area, then reverse the direction for ALL objects and drop them down 1 line.

Not sure if this is the best way to do it, in terms of constantly getting the array of enemy objects and looping through them? Probably not thinking with a modern coders head here?..

Anyways, thanks for any pointers. Paul

I’m fairly new to unity as well, but here i go.

I would make a parent gameObject, then attach each enemy to this parent object. This way you can move the parent gameObject all the enemies will move at one time.

Remember after you do this, the position of the child is now relative to the parent.

** this code was not tested.

GameObject parentGameObject = new GameObject();
for (var x=0; x<4; x++) {
 GameObject enemyShip = Instantiate(enemyPrefab, enemyPositions[x], Quaternion.identity) as GameObject;
enemyShip.transform.parent = parentGameObject.transform
 }

Thanks, I’ll give it a go later!

Hmm… I’m still confused sorry :frowning:

I created an empty object and called it enemyContainer. I then made a script and added it to it (below);

using UnityEngine;
using System.Collections.Generic;

public class enemyContainerScript : MonoBehaviour {
 
 GameObject enemyContainer = new GameObject();
 public GameObject enemyPrefab;
 
 // Use this for initialization
 void Start () {
 List<Vector3> enemyPositions = new List<Vector3>();
 
 enemyPositions.Add(new Vector3(-22,15,-23));
 enemyPositions.Add(new Vector3(-18,15,-23));
 enemyPositions.Add(new Vector3(-22,11,-23));
 enemyPositions.Add(new Vector3(-18,11,-23));
 
 for (var x=0; x<4; x++) {
 GameObject enemyShip = Instantiate(enemyPrefab, enemyPositions[x], Quaternion.identity) as GameObject;
 enemyShip.transform.parent = enemyContainer.transform;
 }
 }

But though I get 1 ship instantiated (not the 4), I get errors like;

ArgumentException: You are not allowed to call Internal_CreateGameObject when declaring a variable.
Move it to the line after without a variable declaration.

If you have any more ideas, I would really appreciate it.

Many thanks, Paul