Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice
  3. Improve your project's performance with our new guide on profiling in Unity.
    Dismiss Notice

Switching Prefabs at Runtime

Discussion in 'Scripting' started by marty, Dec 28, 2007.

  1. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Is it possible to switch a prefab at runtime using Javascript?
     
  2. Omar Rojo

    Omar Rojo

    Joined:
    Jan 4, 2007
    Posts:
    494
    Yeah, but.. what kind of switch ?

    Code (csharp):
    1.  
    2. // Set in the inspector
    3. var prefab1 : GameObject;
    4. var prefab2 : GameObject;
    5.  
    6. // Prefab to use
    7. private var prefab : GameObject;
    8.  
    9. function Start ()
    10. {
    11.    prefab = prefab1;
    12. }
    13.  
    14. function Update ()
    15. {
    16.    // Use the first prefab when pressing 1 and the second prefab when pressing 2..
    17.    if ( Input.GetKeyDown ("1") )
    18.       prefab = prefab1;
    19.    if ( Input.GetKeyDown ("2") )
    20.       prefab = prefab2;
    21.  
    22.  
    23.    if ( /* Something happens ?? */ )
    24.       Instantiate (prefab, transform.position, Quaternion.identity);
    25. }
    26.  
    That is the main idea only !!

    .org
     
    Justice0Juic3 likes this.
  3. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    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.
     
  4. Omar Rojo

    Omar Rojo

    Joined:
    Jan 4, 2007
    Posts:
    494
    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
     
  5. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    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?
     
  6. Omar Rojo

    Omar Rojo

    Joined:
    Jan 4, 2007
    Posts:
    494
    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
     
  7. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    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!
     
  8. Omar Rojo

    Omar Rojo

    Joined:
    Jan 4, 2007
    Posts:
    494
    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
     
  9. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,355
    I don't think the prefab concept extends to runtime. After you start the game everything is just a GO with components...
     
  10. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Thanks for the confirmation, Matt.

    That's pretty much what I've come to realize.

    It's a shame though. ;-)
     
  11. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Usually what i do is simply:

    Instantiate (replacementPrefab, transform.position, transform.rotation);
    Destroy (gameObject);

    What exactly is it you want to do?
     
  12. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    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.
     
  13. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    What exactly didn't work in this approach?
     
  14. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    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.
     
  15. kavanavak

    kavanavak

    Joined:
    Sep 30, 2015
    Posts:
    54
    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)
     
  16. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,363
    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

     
    kavanavak likes this.
  17. kavanavak

    kavanavak

    Joined:
    Sep 30, 2015
    Posts:
    54
    Are you using instantiate to do the replacement?
     
  18. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,363
    Yepp, I destroy the old and instance a new
     
unityunity