J
J
jdanilaj2021-06-17 10:36:14
C++ / C#
jdanilaj, 2021-06-17 10:36:14

How to switch between characters in unity?

It is necessary that when the button is pressed, the characters switch between themselves. But I had a bug, character 2 does not appear in the place of character 1, but in its original place.

Here is the switch code that is attached to Players, and char1 and char2 are children:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Switzer : MonoBehavior
{
public GameObject char1, char2;

int charOn = 1;
void Start()
{
char1.gameObject.SetActive(true);
char2.gameObject.SetActive(false);
}

public void SwitchCharecter()
{
switch(charOn)
{
case 1:
charOn = 2;

char1.gameObject.SetActive(false);
char2.gameObject.SetActive(true);
break;
case 2:
charOn = 1;
char1.gameObject.SetActive(true);
char2.gameObject.SetActive(false);
break;
}
}
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Korotenko, 2021-06-17
@firedragon

It seems to me that this is not a bug
Read the parameters of the hidden object and set them for the displayed one

V
Vitaly Kachan, 2021-06-17
@MANAB

So set the position of the 2nd character, like the 1st.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question