Search Unity

Swap model/prefab on button click

Discussion in 'Scripting' started by pixellegolas, Jun 16, 2018.

  1. pixellegolas

    pixellegolas

    Joined:
    Apr 10, 2011
    Posts:
    223
    Hi! I want to have a main model in game and when pressing a button it should swap to another model in same place. I basically have 3 models that I want to switch between. I also want them to switch colors with another button :)

    Is it best to create one prefab with 3 models that I turn on/off or make 3 prefabs?

    What I found code-wise is this example:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6.  
    7. public class ModelSwap : MonoBehaviour {
    8.  
    9.     public GameObject[] modelArray;
    10.     private int modelNumber;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.  
    15.         modelNumber = 0;
    16.         ModelSwitch();
    17.  
    18.     }
    19.  
    20.     void ModelSwitch()
    21.     {
    22.         for (int x = 0; x < modelArray.Length; x++)
    23.         {
    24.             if (x == modelNumber)
    25.             {
    26.                 modelArray[x].SetActive(true);
    27.             }
    28.             else
    29.             {
    30.                 modelArray[x].SetActive(false);
    31.             }
    32.         }
    33.         modelNumber += 1;
    34.         if (modelNumber > modelArray.Length - 1)
    35.         {
    36.             modelNumber = 0;
    37.         }
    38.     }
    39.  
    40.  
    41.  
    42.     // Update is called once per frame
    43.     void Update () {
    44.  
    45.         if (Input.GetButtonDown("Jump"))
    46.         {
    47.             Debug.Log("Hello");
    48.             ModelSwitch();
    49.         }
    50.  
    51.     }
    52. }
    What I can see in inspector is that the prefabs are switching place, but in the actual game the model stays the same

    Thanks!
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,702
    That code assumes that the GameObjects assigned to modelArray are in your scene, typically as children of the main GameObject (e.g., three separate models, one of which will be active at a time).

    If you expect to be switching frequently and have enough memory to hold all three in memory, make all three children in the same parent GameObject, like the code you posted above expects.

    Also, move the parent GameObject. This will keep the three models in the proper location.
     
  3. pixellegolas

    pixellegolas

    Joined:
    Apr 10, 2011
    Posts:
    223
    Thanks! Can I have an Empty with 3 objects as children and move the empty? I am trying to change material of the children but GetComponentsInChildren does not seem to work good.

    I have managed to have en empty that moves and shows a default cube. When pressing space it swithes to another model. I had a code that actually switched material on the models but not when I made them children of an empty. It nags about that the empty does not contain renderer.

    What my whole scenario is, is the following:

    I want to have 3 objects to switch between
    They should be able to have 3 different materials
    I also want to assign a tag on the object for each material
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,702
    Yes.

    If the child is inactive, GetComponentsInChildren won't be able to find components on it. There are other ways to find components on inactive children (e.g., manually traverse the Transform hierarchy), but it might be better instead to assign the components to public variables on the main GameObject at design time. This way you don't need to do any searching.