instantiate newbie

Hi All,

I’m trying to instantiate an object from a c# script which does not inherit from MonoBehaviour (a custom class that handles networking code).

I’m not sure how should I call it, this is what I have right now:

GameObject obj = (GameObject)MonoBehaviour.Instantiate(prefab, position, rotation);

This compiles just fine, but on runtime it throws an exception:

InvalidCastException: Cannot cast from source type to destination type

How can you instantiate an object from a non monobehaviour class?

thanks for any help.

Ok… I would’ve thought that Instantiate does more than just call the constructor… Is Instantiate a special c# method? (I’m new to c#)

thanks…

I am interested in this too.
Why can’t the keyword ‘new’ be used?

Transform myPrefabInstance = new MyPrefab();

?

new Whatever() can be used if you have a class with that name and a default constructor. A GameObject, what you place in the scene, is usually more than just a class. It has at least a Transform object attached to it, so you have to use Instantiate.

Ah - got it. So a gameObject needs a bunch of other stuff to be visible in a scene, not just to run the constructor?

Yeah, the addition of GameObjects to the scene have a whole whack of other house keeping involved. So if you want to create new scene GameObjects, use Instantiate. If you want to create new instances of your own custom classes (that do not inherit from GameObject or MonoBehaviour) then you can use your own constructors or methods or whatever you want.

Ok… so how would I use Instantiate from a non-monobehaviour subclass? I think my problem is just related to some cast that I’m doing the wrong way…

If you´re having cast problems, I suppose you´re using C#, then

using UnityEngine;

...

GameObject x = GameObject.Instantiate(prefab) as GameObject;

thanks dart,
it compiles and runs with no warnings/errors, but it does not return a valid object:

GameObject obj = GameObject.Instantiate(prefab, position, rotation) as GameObject;

after that, obj is null. The prefab is however, instantiated (it appears in the screen and in the object list in the editor)…

As soon as I remove the “as GameObject” part of the code, it works (it returns a not null object), but then it fails again when trying to call a method like “SendMessage” or “GetComponent”…

Hello all,

Ok, I did a deep search right now and I have the same problem.
I have a GameObject to instantiate and I’m throwing to a variable, like everyone else (in C#). With Javascript, I could make it work, but in C#, I just can’t (maybe something stupid I’m doing).

BUT I find other posts with same problems and nobody answered/solved the problem.

http://forum.unity3d.com/viewtopic.php?t=60225&highlight=instantiate

http://forum.unity3d.com/viewtopic.php?t=52931&highlight=cannot+cast+source+type+destination+type

http://forum.unity3d.com/viewtopic.php?t=48576&highlight=cannot+cast+source+type+destination+type

What I’m trying do to (example):

public GameObject enemy;

void Update(){
    if (someVar == 1){
        GameObject newEnemy = GameObject.Instantiate(enemy, transform.position, transform.rotation) as GameObject;
        newEnemy.SendMessage("SetField", 1);
    }
}

This returns me this error when the GameObject got instantiated (in line of the SendMessage):
NullReferenceException: Object reference not set to an instance of an object

I tried this line too:

GameObject newEnemy = (GameObject)GameObject.Instantiate(enemy, transform.position, transform.rotation) as GameObject;

And this too:

GameObject newEnemy = (GameObject)Instantiate(enemy, transform.position, transform.rotation);

All of this compile with no errors, but when I play this thing and the Object got instantiated, it returns that error above or the
InvalidCastException: Cannot cast from source type to destination type

Anybody has a workaround for this?
I searched the forum and looks like lots of people had problems with that, but I did not see an answer. I tried even using that typeof but no sucess.

I really think its something stupid I’m doing… lol

What is the type on the “prefab” member? If its declared as a component type, then Instantiate will return that component. If its declared as a GameObject then it will return a GameObject.

public CharacterController Prefab1;
public GameObject Prefab2;

...
// This will work
GameObject go1 = (GameObject)Instantiate(Prefab2);

// This won't
GameObject go2 = (GameObject)Instantiate(Prefab1);

// This will work
CharacterController cc2 = (CharacterController)Instantiate(Prefab1);

As an aside, there are two typecast methods in C#. The one I’m using there above will throw a runtime exception if the cast is invalid. The “as” typecast will return null instead. If you use the “as” typecast, you should if( !=null) on the return value before you try to do anything with it.

Hey dalep, thanks for the tip, but I already know this.
If you check my example code, you can see that this is exacly what I’m doing.

The problem is when I try to use SendMessage or get another Component… that object itself got instantiate sucessfully. :wink:

Well xandeck, I was responding more to stuff farther up the thread.

Try taking all those typecasts off and return from Instantiate into an Object. Then print(Object); and see what type is getting returned.

Ok, thanks for the help … I did another way to find what I was looking for, I will search this in the future. :smile: