Yeah, but.. what kind of switch ? Code (csharp): // Set in the inspector var prefab1 : GameObject; var prefab2 : GameObject; // Prefab to use private var prefab : GameObject; function Start () { prefab = prefab1; } function Update () { // Use the first prefab when pressing 1 and the second prefab when pressing 2.. if ( Input.GetKeyDown ("1") ) prefab = prefab1; if ( Input.GetKeyDown ("2") ) prefab = prefab2; if ( /* Something happens ?? */ ) Instantiate (prefab, transform.position, Quaternion.identity); } That is the main idea only !! .org
Thanks, Omar. The thing is, I'm not looking to instantiate a new prefab but rather, to simply replace the one already being used by a gameobject. That way, I could otherwise keeping the GameObject's physics, et al, intact. I'd literally like to be able to say something like: this.GameObject = newPrefab; - although I can't, since GameObject is read-only.
So... you want to replace the object rendered right ? You should use an EGO as a parent then and the GO with the mesh as a child, so all your physics, configurations and stuff goes in the parent, and when you want to switch to another model, just delete the child GO and then create a new child with the other prefab, is that ? .org
Thanks, Omar. I do appreciate your help. I'm curious though, does anyone know if there is a way to simply change the prefab that a given GameObject is based on, dynamically, at runtime?
Well you can change the material and the mesh of the given object... but i think its more confusing... If they use the same material then is only a fact of changing the mesh used. The mesh is found under the MeshFilter (component). .org
Yeah, that's what I'm doing now and it's a real drag having to change both mesh and material. I guess GameObjects are just containers for components. Period. Oh well. Thanks again, Omar!
No no no, is very easy dont get scared by my words, just cache all meshes and materials and then just change them, that is all. .org
I don't think the prefab concept extends to runtime. After you start the game everything is just a GO with components...
Thanks for the confirmation, Matt. That's pretty much what I've come to realize. It's a shame though. ;-)
Usually what i do is simply: Instantiate (replacementPrefab, transform.position, transform.rotation); Destroy (gameObject); What exactly is it you want to do?
I tried instance-destroy-ing my object, but there were problems specific to what I'm doing. It seemed like the only thing that would work would be to actually tell the game object to point to a different prefab, but otherwise not change. But, alas, that isn't an option. For what it's worth, what I ended up doing was having multiple complex objects (i.e. mesh+material+scripts+etc) inside an empty GO, all but one of which were render-disabled. to change states, I just made a different object render-enabled and all other disabled. It's expensive, but it works.
It worked. In fact, I've gone back to it in the name of performance. It's just a bit more involved than Id like it to be - the reason stemming from the fact that I use 3ds Max, which is natively Z-up.
Has there been an update on this? I'm trying to do the same thing as Marty, replace a current GO with a new one. I could do all the individual replacements, mesh material etc, but the new GO has a ton of children and I would rather just swap a dummy placeholder GO for the new on at runtime with a single command rather than replacing tons of elements and re-parenting etc (or instantiating new and destroying old)
Replacing a single prefab isn't that expensive as long it's not a too complex one. Here I stress test our team switch which replaces first person hands