Instantiate(prefab) as MyComponent vs. GetComponent<MyComponent >()

Hello,
this is beginers question but I would like to understand what is going on.

Is there difference between:

GameObject prefab;
MyComponent comp = Instantiate(prefab) as MyComponent;

and this

GameObject prefab;
GameObject obj = Instantiate(prefab) as GameObject;
MyComponent comp = obj.GetComponent<MyComponent>();

?
I mean instead of extra line?
Also, what happens in the first case if prefab has no MyComponent attached to it? Would Instantiate(prefab) fail?

If first case works the same as second, why cant I just typecast GameObject to its component instead of using GetComponent() all the time?
Like this:

MyComponent comp = obj as MyComponent;

Thank you for helping me understand basics better.

That doesn’t compile for me. I suspect you are thinking of something more like:

MyComponent prefab;
MyComponent comp = Instantiate(prefab) as MyComponent;

(I changed the type of the “prefab” variable.) This second version works because the Instantiate() function has a generic version,
public static T Instantiate(T original)
which lets you use a component-type variable as the thing you want to clone and then returns the same component-type. In fact, the “as MyComponent” part is redundant and doesn’t do anything, because the return type is already MyComponent before the cast.

You can be more explicit by writing it as

MyComponent comp = Instantiate<MyComponent>(prefab)

(Speaking generally, though, saying “as SomeType” returns null if the thing you are casting isn’t a valid instance of SomeType.)

2 Likes

Because it’s not the same type, you are try to make an Box into an Apple.

GameObject = Box
Componnent = Apple

//a blueprint of a Box with an Apple attached to it
GameObject prefab;

//GameObject obj is an empty Box no identity

//so I create a new Box with an Apple out of the blueprint and make obj become the new box i just created
GameObject obj = Instantiate(prefab);

//MyComponent comp is an empty Apple also no identity so I get the Apple from obj box and give an identity to comp
MyComponent comp = obj.GetComponent<MyComponent>();

//if obj box doesnt have an apple, MyComponent comp will never have identity
 //a blueprint of a box with an Apple attached to it
GameObject prefab;

//MyComponent comp doesn't have identity so I create a box out of the blueprint and 
//magically turn into an apple now MyComponent comp is a Apple? hm....... that doesnt seem right.... say the debugger
MyComponent comp = Instantiate(prefab) as MyComponent;
//turn an empty apple into a empty box that has been turn to an apple. Angry error messenger
MyComponent comp = obj as MyComponent;
1 Like

This is reason why I asked this question. Because it doesnt seem right but it should work. This is what they do in this tutorial…or is it? I just noticed that second example should have been this:

MyComponent prefab;
MyComponent comp = Instantiate(prefab) as MyComponent;

Now Im super cofused. They make prefab and put it into Maze type field in the inspector? Maze is supposed to be component type, no?

    public Maze mazePrefab;

    private Maze mazeInstance;

    private void BeginGame () {
        mazeInstance = Instantiate(mazePrefab) as Maze;
    }

    private void RestartGame () {
        Destroy(mazeInstance.gameObject);
        BeginGame();
    }

we need to look into Maze itself. what the secret wink wink;)

Yes, this is what I ment indeed. Thank you for correcting me before answering.

from API makes more sense now.

So with

MyComponent prefab;
MyComponent comp = Instantiate(prefab) as MyComponent;

you clone component and GameObject is cloned with it but with

GameObject prefab;
GameObject obj = Instantiate(prefab) as GameObject;

you clone GameObject itself?

And how comes you can store Prefab in component type field?

EDIT: Ok, asnwer on question “What is a type of prefab” here I just found basicaly explaines that:

2 Likes