Question about enemy GameObject

Hi everyone, there’s a small issue that has been bothering me a bit from from the previous Block Breaker section and appears now as well. in the enemySpawner script we created a public GameObject called enemyPrefab that holds the enemy’s spaceship prefab. Two code lines down the road we are creating another GameObject which is initialized with the Instantiate function, while send enemyPrefab as one of its parameters. Now my question is, isn’t that an unnecessary duplication? I mean, it causes no harm, yet why don’t we the already-declared enemyPrefab GameObject?

As a second question (not sure if it has any relation to my first), I would really appreciate a bit of clarification on the diference between GameObject, gameObject, and ‘this’ (also, whats is this Object type that Instantiate returns?). I have a small mess in my head regarding these terms as I’m not quite sure which is attached to what, when do i use each of them etc. Perhaps it could help me understand my first problem.

Thanks a lot!

1 Like

I always find it hard to be clear when explaining these things, but I’ll try my best anyway, and if someone else can make it clearer, by all means do it :wink:

Let’s answer your second question first, since indeed it can make the answer to the first one clearer.

GameObject is a class, or a type. Everything in a Unity game is of type GameObject. As a class, GameObject has variables and functions. Some of those functions are static, meaning you can call them without instantiating an object of type GameObject (hopefully this isn’t too confusing yet).

So you would use the keyword GameObject either to create an object of type GameObject, as in:

GameObject enemy = new GameObject("Enemy");

or to call a static function of the GameObject class, as in:

enemy = GameObject.FindObjectOfType<Enemy>();

Secondly, gameObject, written in a script file, refers to the instance of the GameObject that the script is attached to. For example you may have several enemy ships in the scene, each with an Enemy script attached. So if this Enemy script says:

if (gameObject.transform.position.x > 10f) {
    Destroy(gameObject);
}

it means: look for the instance of the ship this script is attached to, see if its position is more than 10 units in x, and if so, destroy that instance.

Now this refers to the script component itself. If you write Destroy(this) in a script, when you run the game the script will get removed from the GameObject it was attached to, but the object itself will remain there.

So, on to your first question. At first we write:

public GameObject enemyPrefab;

This exposes a variable of type GameObject to the world (and more importantly, the inspector).
Then we say:

GameObject enemy = GameObject.Instantiate(enemyPrefab);

Here, we create a new instance of the enemyPrefab GameObject, called “enemy”. We basically use the enemyPrefab GameObject as a template for all the instances in our game.

I hope that’s somewhat clear… maybe… I’m not sure myself anymore. Anyway, hope that helps a bit.

2 Likes

That’s about as clear as it can possibly get (: Thank you very much, it really helped a lot!

1 Like