Questions about Instantiate...As and this/gameObject

I have been wondering a few things.

  1. In several projects we have Instantiated objects using with the “as” keyword. What exactly does this accomplish, especially when we have declared the type of the variable as the same object type. I have removed the “as ObjectType” in each case, and everything has functioned as it was supposed to.

  2. We have seemed to use the variable “gameObject” in place of what I would traditionally use “this” for. Is it just a “this” for Unity GameObjects, or is it more (or less)?

  3. (And why I finally asked in this lesson.) Why are we seem to be setting the parent of the attacker to be the transform object on the Spawner instead of the Spawner itself?

Hey, im gonna try and give you a kind of a placeholder answer until someone gives you a better one :smile: .

  1. I believe you are talking about casting types. Similar to when we have an int variable for score and we display it with scoreText.text = score.toString. This to string is casting the int into a string and with ‘as’ you are making sure that the thing you are instantiating gets instantiated as the type you want it to tho i think you maybe even dont need that in new versions of Visual Studio. If the code becomes grey, means its not needed.

  2. Ben provided this link to an explatation on this vs gameObject in one lections in the unity3D course:
    https://blog.gamedev.tv/this-vs-gameobject-in-unity/

  3. Sorry, im not quite sure what this part is referring to.

Hope it somewhat helps

1 Like

Thank you for the link, that helped explain that part.

For the first part, I am familiar with casting, I believe Instantiate returns an object of the type being Instantiated. If that is not true, then I understand, but the docs make me think it does. So why are we always casting things to what they are? If we cast a Defender to a GameObject to use something hidden by the Defender class, I’d get it.

Hi,

In past versions of Unity the Instantiate method returned an object of type Object. This was changed in Unity 2017, I think. It now returns a GameObject object, so casting is not necessary anymore. Game developers who have been using Unity for years still cast out of habit.

Have your questions been answered? :slight_smile:


See also:

1 Like

Other than my third question, they have.

To clarify, they do not return an object of the same type as the prefab?

A prefab is always an object of type GameObject.

I’m not sure if I understood this question correctly but the position property is part of the Transform object, not of the Spawner object.

In the lesson we did

‘’’
myAttacker.transform.parent = transform;
‘’’
I was wondering why we set the parent to the transform object instead of the object which had the transform object as it’s child.

Unfortunately, I’m not sure what you mean by “the object which had the transform object as it’s child”? Which object is “the transform object”? The names of game objects you mean could be helpful.

We have the attacker, and we want to parent it to another game object to keep our Hierarchy tidy. We can achieve that by assigning a Transform object to the transform.parent property of our attacker.

For instance, if we have an Attackers game object in our Hierarchy, we could instantiate our attacker and parent it to the Attackers game object. If our spawner object is attached to the Attackers game object, we can access the desired Transform object via transform.

To which Transform object is the single transform referring to in the line you pasted here?

We are trying to put a spawned attacker into the hierarchy of the object creating it to act as the “folder” for all attackers created in that lane.

On the creation of an attacker, via scripting in the SpawnAttacker script attached to the object that is doing the spawning, we say that the transform.parent = the transform of the creating object the one we want to use as a folder.

so:

SpawnedObject.tranform.parent = transform (of the object the script is on, the Spawner)

My question is this: In the component list the Spawner has a transform. It can not be deleted (which makes sense since it represents foundational properties like position), but is it the object itself, which it seems to be in the code we wrote for the lesson, or is it a component of the true object.

What would be the difference, and meaning between writing:

SpawnedObject.transform.parent = transform (of the Spawner where this code is located)

and

SpawnedObject.transform.parent = gameObject (the Spawner where this code is located)

If I am still not making sense, I will let it go.

Thanks.

In Unity, each GameObject object also “owns” a Transform object/component. If you assign a script to a game object in the Inspector, you can access the aforementioned Transform object via transform due to the fact that your script inherits from MonoBehaviour. And by assigning the script to the Inspector, Unity automatically assigns the reference of the Transform object of the game object to which the script is attached to the transform property that your script inherits from the MonoBehaviour object.

If your Spawner instance is attached to a game object named “ABC” (or whatever), the transform property in this Spawner instance references the Transform object of the “ABC” game object. And gameObject references the GameObject object “ABC”.

gameObject, an object of type GameObject, cannot be assigned to parent because parent is of type Transform.

If that does not answer your question, I would suggest to simply test your code to see the difference between both lines.

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

Privacy & Terms