Cant understand what information im sending

Hello.So my question is in the picture the red arrow shows the line

  1. dont get it how the Instantiate will work now?
    We just put it in this newEnemy variable i get it and after this blank mind.
  2. What info is in waveConfig in the bracket cant understand.What i am sending with this method?

Thank you.

Hi Shaktillar,

Thank you for your questions.

  1. What exactly would you like to know about Instantiate? That method does not have anything to do with newEnemy.GetComponent().SetWaveConfig(waveConfig);.

  2. waveConfig is a variable of type WaveConfig. See line 8. We assigned a WaveConfig object to the exposed field in the Inspector in the Unity editor. When calling SetWaveConfig, we pass on the value of waveConfig to the SetWaveConfig method meaning the reference (“link”) to the assigned WaveConfig object. If we didn’t assign anything, we would pass on null. We do not pass on the waveConfig variable, just its value.

Did this clear it up for you? :slight_smile:

See also:

I dont understand what is this line doing.
What i see is that im accesing the EnemyPathing script then im executing the SetWaveConfig(waveConfig)

Where is happening the Instantiate of the enemy plane?
Before we write" var newEnemy = " it was clear that we are Instantiating the enemy plane.
After this im lost.All i understand we are accesing the EnemyPathing script then executing the SetWaveConfig(waveConfig)
Thank you.

I have to admit that I’m slightly confused because what you described is exactly what is happening in the code.

  1. We instantiate a new enemy and assign it to the newEnemy variable.
  2. Then we call GetComponent() on the object assigned to newEnemy to get the EnemyPathing component.
  3. Then we call SetWaveConfig on that EnemyPathing object and pass on the WaveConfig object assigned to waveConfig.

There isn’t anything else happening.

  1. We instantiate a new enemy and assign it to the newEnemy variable.

Where is the code that instatiates the newEnemy variable?
All what i understand is that we are assign to the newEnemy variable but where we call this variable?

In line 24 in the picture above if i write

step by step

  1. newEnemy — we are accesing the variable
    2)GetComponent(). — get the component of enemy pathing
    3)SetWaveConfig(waveConfig); ---- run the method

Im correct?

Unfortunately, I cannot copy and paste text from screenshots. Look for the Instantiate method in your code. That’s the method which instantiates/spawns a new enemy.

Regarding your “step by step” question, yes, what you described is correct. :slight_smile:

Ooook after experimenting with the code i find my answer.

I know this is the code what instatiates the enemy plane ,what confused me is that if we assigns this to a variable newEnemy

var newEnemy = Instantiate(
then this will still create the gameobject and instantiates the plane.So if i understand now correctly when we create this new variable newEnemy we are creating a new gameObject which will instantiate the plane?

Its like this example:

GameObject pathPrefab;
GameObject laserPrefab;


GameObject newEnemy = Instantiate(

For me it seems the same

Variables themselves don’t do anything. They are just “there” like a box which may or may not contain something. The value of an “empty” variable is null. Maybe you’ve already encountered a NullReferenceException error in this course.

The Instantiate method creates a new object. And the “link” to the object gets assigned to the newEnemy variable. This way, we can keep track of the new enemy and we can access it.

Test the following:

GameObject newEnemy;

If you understood the idea behind variables and assigning values to variables, you’ll be able to answer this without running the two lines above: Why will these two lines throw a NullReferenceException error?

I understand Variables themselves don’t do anything. They are just “there” like a box which may or may not contain something. The value of an “empty” variable is null .

So i was correct and this is the instantiate method which creates the new object?

var newEnemy = Instantiate(

  1. GameObject newEnemy;

Didnt run it it seems that newEnemy here is empty.Im right?

Yes, you are right. :slight_smile:

This would work, too, but we would lose the reference (“link”) to the new enemy:


We do not need var newEnemy if we just want to spawn a new object.

Exactly. The code would run but we would get an error because, as you said, newEnemy does not have any reference assigned, it is “empty”.

Everything is clear but tell me please why is instantiate working if i assigned to a variable.

I understand that if i write without the referenece then im asking the program to instatiate the prefab.
Thats clear.Super clear.
Now when i assign it and we write
var newEnemy = then why the program is still running the instatiation?For me its like nothing must happend here because i just assigned.
:smiley: i hope you get what is my dilema here.

From my understanding, it’s because you’re assigning the variable to the result of the Instantiate method call, and in order for your code to determine what that variable is, it has to run the instantiate method and then assign the result of that to the variable, which gives the desired effect of both instantiating something and allowing us to manipulate what was instantiated in our code.

Tristan is right.

You probably saw “void” in your code, for example, in Start or Update or your own methods. When you call these methods, they do something, and that’s it. They do not return anything.

The Instantiate method does something too, just like your own methods, but it also returns an object. You can tell that a method returns an object by taking a look at the method signature. Here, the signature comes with a T instead of void. This means, it returns returns an object of type T, whatever T means. In your case, it returns a GameObject object.

From the API:


Whenever you want to make a method return something, for example, a method that calculates something, you need a return type.

Ohh yes finally i understand.Thanks to Tristan_Hedges and Nina for the explanation.(my english just not good enough)

1 Like

Your English is absolutely fine, I assure you :slight_smile:

Don’t worry, Shaktillar. I understood you perfectly fine (I hope). And your questions were great. I had the same questions when I started with Unity and C#. :slight_smile:

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

Privacy & Terms