In Unity with C#, I am trying to Instantiate from a prefab and assign a script to the new object. To do this, I believe that I need to have the new object cast as a GameObject. However, the return type of Instantiate is UnityEngine.Object
From the Unity manual:
public GameObject wreck;
...
void KillSelf () (
...
GameObject wreckClone = (GameObject) Instantiate(wreck, transform.position, transform.rotation);
This, however, results in an InvalidCastException. I have seen it mentioned on this forum to try this instead:
UnityEngine.Object uo = Instantiate(...
GameObject go = (GameObject)go;
This, however, results in the same exception.
I have seen some posts suggesting trying to cast with:
...Instantiate(...) as GameObject;
This syntax does not, however, appear to be supported by the compiler.
This is pretty confusing. I'm copying small bits of code out of (presumably correct) manuals and stack overflow answers, and they just don't seem to function.
I'm not sure what information would be necessary to help solve this puzzle, so just ask me for what you need.
So far i have tried logging the object instantiated before trying to cast it. It shows up as ThrownFood(clone)(UnityEngine.transform). I don't know why it would say transform though. I don't have anything telling it to be cast as a transform.