Instantiate and type cast errors

public GameObject myGameObject;


Object obj = Instantiate(myGameObject);
Object obj2 = obj;

I must be missing something really obvious. Does anyone know why the last line would throw an InvalidCastException? ("Cannot cast from source type to destination type.")

How sure are you that the exception is thrown in the last line?

Exceptions in C have line numbers to the the return site not the call site in the stack trace. (In javascript this works correctly.)

So most likely the cast error happens in the line before. (The line with Instantiate)

I am very sure. :wink:

After restarting Unity everything worked fine. I have had oddness like this happen before. I would suspect it has something to do with the C# compiler, but I can't reliably repeat it. (The Instantiate() method, in particular, seems to occassionally behave strangely.)

It looks like this still happens, even with the latest version of Unity (2.6). Simply restarting Unity makes the problem go away.

Yo! Hello . tsphillips i have same errors as yours but i cant fix mine. . :( can you please help me guys. Im a noob at unity and only depending on tutorials in the web. here is the link of mu question. Hoping for your response guys. thank you.