- Home /
Problem renaming game objects
Hi, all. I'm running into a problem with renaming game objects. I've written a function to spawn characters into my scene:
void spawnCharacters(int place, UnityEngine.Object model, Vector3 location, string objName)
{
characters[place] = (GameObject)Instantiate (model, location, Quaternion.identity);
characters[place].name = objName;
}
Which is what I found suggested after poking around on the web. However, when I try to run it, I get this error:
InvalidCastException: Cannot cast from source type to destination type.
What's going on?
Thanks in advance for your help.
Answer by Tarlius · Mar 15, 2013 at 08:47 AM
Since the exception is a casting problem, I'm going to take a guess that the naming has nothing to do with your problem (since theres no cast).
What you are passing in is probably not a GameObject. You should probably set the function to only accept GameObject and not UnityEngine.Object, then fix the next compiler error you'll get from passing something that isn't a GameObject to the function (or add a check to ensure that the Object is a GameObject if this is appropriate, although from the given code it seems unlikely)
With no knowledge of your project, and only the code to go on, are you perhaps passing a mesh and not a prefab to this function?