[英]Unity Switching between gameobjects
I've got weird issue with my project, I'm trying to change between 2 gameobjects -villager to werewolf (both are children of the same empty gameobject) by pressing a key.我的项目有一个奇怪的问题,我试图通过按键在 2 个游戏对象 - 村民到狼人(都是同一个空游戏对象的孩子)之间进行切换。 for some reason it switching only one time and "stuck" at werewolf gameobject without switching back to villager.
由于某种原因,它只切换一次并“卡在”狼人游戏对象上,而没有切换回村民。 it's the second time I'm trying to handle the getter/setter.
这是我第二次尝试处理 getter/setter。
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class WerewolfTransform : MonoBehaviour
{
public GameObject villager;
public GameObject wereWolf;
public bool isWerewolf;
private void Awake()
{
villager.SetActive(true);
wereWolf.SetActive(false);
}
public bool getIsWereWolf
{
set
{
if (isWerewolf != value)
{
isWerewolf = value;
villager.SetActive(false);
wereWolf.SetActive(true);
}
else
{
villager.SetActive(true);
wereWolf.SetActive(false);
}
}
get
{
return isWerewolf;
}
}
private void Update()
{
Transformation();
}
public void Transformation()
{
if (Input.GetKeyDown(KeyCode.T))
{
wereWolf.transform.position = villager.transform.position;
getIsWereWolf = !getIsWereWolf;
}
}
}
Well whenever you change the value the first case kicks in and you hard set好吧,每当您更改值时,第一个案例就会启动并且您很难设置
villager.SetActive(false);
wereWolf.SetActive(true);
no matter what value you are actually changing it to.无论您实际将其更改为什么值。
You would rather do it like eg你宁愿这样做,例如
public bool getIsWereWolf
{
set
{
isWerewolf = value;
villager.SetActive(!isWerewolf);
wereWolf.SetActive(isWerewolf);
}
get => isWerewolf;
}
To be sure to have a consistent initial state I would then also use it in Awake
为了确保具有一致的初始 state 我也会在
Awake
中使用它
private void Awake()
{
getIsWerewolf = false;
}
First of all thanks!首先感谢! And I did something very similar to what u have suggested and it worked!
我做了一些与你所建议的非常相似的事情并且它奏效了! Ill posted what I've done ( the bool didnt worked as i wanted to I went for another direction)
生病发布我所做的(布尔没有按我的意愿工作,我去了另一个方向)
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using Photon.Pun; public class WerewolfTransform: MonoBehaviour { public GameObject villager; public GameObject wereWolf; //public bool isWerewolf; public int formSwitch; private void Awake() { villager.SetActive(true); wereWolf.SetActive(false); formSwitch = 1; } private void Update() { Transformation(); } public void Transformation() { if (Input.GetKeyDown(KeyCode.T) && formSwitch == 1) { villager.SetActive(false); wereWolf.SetActive(true); formSwitch = 2; wereWolf.transform.position = villager.transform.position; wereWolf.transform.rotation = villager.transform.rotation; } else if (Input.GetKeyDown(KeyCode.T) && formSwitch == 2) { villager.SetActive(true); wereWolf.SetActive(false); formSwitch = 1; villager.transform.position = wereWolf.transform.position; villager.transform.rotation = wereWolf.transform.rotation; } } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.