Switching out GameObject with Prefab?
I want to switch a game object by replacing it with another prefab. I found an easy tutorial to do that but when it goes past case 3 it only switches case 1 and 2. Any possibility it can switch through each as I click. Here is the link to the tutorial. Time starts 1:10. https://www.youtube.com/watch?v=_B1Lc6tUSx8
public GameObject avatar1, avatar2, avatar3, avatar4, avatar5;
int whichAvatarIsOn = 1;
// Use this for initialization
void Start () {
avatar1.gameObject.SetActive (true);
avatar2.gameObject.SetActive (false);
avatar3.gameObject.SetActive (false);
avatar3.gameObject.SetActive (false);
avatar4.gameObject.SetActive (false);
avatar5.gameObject.SetActive (false);
}
// Update is called once per frame
public void SwitchAvatar ()
{
switch (whichAvatarIsOn) {
case 1:
whichAvatarIsOn = 2;
avatar1.gameObject.SetActive (false);
avatar2.gameObject.SetActive (true);
avatar3.gameObject.SetActive (false);
avatar4.gameObject.SetActive (false);
avatar5.gameObject.SetActive (false);
break;
case 2:
whichAvatarIsOn = 1;
avatar1.gameObject.SetActive (true);
avatar2.gameObject.SetActive (false);
avatar3.gameObject.SetActive (false);
avatar4.gameObject.SetActive (false);
avatar5.gameObject.SetActive (false);
break;
case 3:
whichAvatarIsOn = 4;
avatar1.gameObject.SetActive (false);
avatar2.gameObject.SetActive (false);
avatar3.gameObject.SetActive (true);
avatar4.gameObject.SetActive (false);
avatar5.gameObject.SetActive (false);
break;
case 4:
whichAvatarIsOn = 3;
avatar1.gameObject.SetActive (false);
avatar2.gameObject.SetActive (false);
avatar3.gameObject.SetActive (false);
avatar4.gameObject.SetActive (true);
avatar5.gameObject.SetActive (false);
break;
case 5:
whichAvatarIsOn = 5;
avatar1.gameObject.SetActive (false);
avatar2.gameObject.SetActive (false);
avatar3.gameObject.SetActive (false);
avatar4.gameObject.SetActive (false);
avatar5.gameObject.SetActive (true);
break;
}
}
}
Answer by Hellium · Mar 10, 2018 at 12:22 PM
Here is a complete rework of your solution, but much simpler using an array:
// Drag & drop the avatars into the array
public GameObject[] Avatars ;
private int activeAvatarIndex = -1 ;
// Use this for initialization
void Start ()
{
SwitchAvatar() ;
}
public void SwitchAvatar ()
{
// Disable last avatar
if( activeAvatarIndex >= 0 )
Avatars[activeAvatarIndex].SetActive( false );
// Compute next index
// % is the modulo operator
activeAvatarIndex = (activeAvatarIndex + 1) % Avatars.Length ;
// Enable new avatar
Avatars[activeAvatarIndex].SetActive( true );
}
Answer by Bunny83 · Mar 10, 2018 at 12:19 PM
Uhm your code explicitly switches only between case 1 and 2 unless "whichAvatarIsOn" would be set to 3 or 4 in which case your method only switches between case 3 and 4. If you want to iterate through all cases you want to create an actual chain / loop through all of them. When you execute case 1 you set "whichAvatarIsOn" to "2" so the next time you call it case 2 is executed. Of course you would have to set "whichAvatarIsOn" to "3" in case 2. Likewise case 4 should set it to 5 and case 5 probably should set it back to 1.
Though there's a much simpler and more flexible way. Just use an array
public GameObject[] avatars;
private int whichAvatarIsOn = 0;
void Start()
{
ActivateAvatar(0);
}
public void NextAvatar()
{
ActivateAvatar((whichAvatarIsOn + 1) % avatars.Length);
}
private void ActivateAvatar(int aIndex)
{
whichAvatarIsOn = aIndex;
for(int i = 0; i < avatars.Length; i++)
avatars[i].SetActive(i == aIndex);
}
Your answer
Follow this Question
Related Questions
Switch Statement Gives Error PLEASE HELP!!! 2 Answers
How can I attach a Gameobject from the scene to a prefab on the Assets folder? 0 Answers
Advantages of instantiating prefab vs gameObject? 1 Answer
List of unused game objects and prefabs 1 Answer
Making an endless hallway with random objects spawning 0 Answers