Var newEnemy = Instantiation creates new enemies

Can someone explain how the code below creates new enemies? Is the Instantiate method being executed even if it’s assigned to a variable? (Line 22)

Hi gstmn,

Welcome to our community! :slight_smile:

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Regarding your question, we execute a SpawnAllEnemiesInWave coroutine. There is a for-loop in it which determines how many times the code block of the for-loop gets executed.

In that code block, we instantiate the enemy prefab, give it a position and a rotation (Quaternion.identity). Then we look for the EnemyPathing component attached to the instantiated enemy and pass on a WaveConfig object so the EnemyPathing component can get the data from that WaveConfig object. And in line 27, we call WaitForSeconds to create a delay. Otherwise, a new enemy would get instantiated within milliseconds.

Hope this helps. :slight_smile:


See also;

Thanks Nina for letting me know.

I guess to be more specific with my question, what is the difference between these two lines of code?

  1. Instantiate(preFabObject, transform.position, Quaternion.Identity);

  2. var newObject = Instantiate(preFabObject, transform.position, Quaternion.Identity);

The first line instantiates the preFabObject but the second line also instantiates the preFabObject. I don’t understand how that works the same way as the first line when the instantiate method is inside a variable. Is the Instantiate method executed even if it’s inside a variable and is just storing the preFabObject as newObject?

Here is an example where an object is instantiated and a velocity is applied to the object.

GameObject laser =
Instantiate(lazerPrefab, transform.position, Quaternion.identity)
as GameObject;

laser.GetComponent().velocity = new Vector2(0, projectileSpeed);

So if the lazerPrefab is not assigned to the laser GameObject would we not be able to access the components of the instantiated lazerPreFab? Then to apply the velocity to the lazer we would need to add a separate script that is attached to the lazerPreFab?

Thanks for your help.

Did you try creating a public variable and dragging your prefab into the inspector. like
public GameObject laser; then dragging the prefab in

Everything is set up correctly and is working just fine. Here is the rest of the variables.


[SerializeField] GameObject lazerPrefab;
[SerializeField] float projectileSpeed = 10f;

GameObject laser =
Instantiate(lazerPrefab, transform.position, Quaternion.identity)
as GameObject;

        laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);

I am just trying to understand the difference between code (1) and code (2). I just started coding and took a couple of intro courses in college so there’s some concepts that don’t make sense from what I was shown in class.

(1) GameObject laser =
Instantiate(lazerPrefab, transform.position, Quaternion.identity)
as GameObject;

(2) Instantiate(lazerPrefab, transform.position, Quaternion.identity);

The Instantiate method not only instantiates a game object in the Unity scene but it also returns a C# object. If you assign that returned object (actually, the reference to the object) to a variable, is up to you and won’t affect the behaviour in Unity. If you don’t assign the reference to a variable, it gets lost. The worst thing that could happen is that you cannot find the object again because in C#, everything looks the same. There are no names.

Assigning the returned object makes sense if you want to do something with that object in your code, for example, to destroy it after x seconds or, in your case, to change its velocity.

The lazerPrefab is not the spawned object in your scene. The latter is a new object, thus you cannot simply access the object assigned to lazerPrefab. You must access the instantiated object. In your case, you assigned the object reference (“link” to the object) to laser. Then you are able to access the object via the laser variable.

Each instantiated object has got its own components. The prefab might look like the instantiated game object but they are not the same. For this reason, if you want to change something in the instantiated laser object, you need to access that object.

Did that make sense?

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

Privacy & Terms