Unity C# bool resetting to false
本文关键字:to false resetting bool Unity | 更新日期: 2023-09-27 18:08:43
我有一个布尔变量,当我想让场景的某些部分向上移动时,我将其设置为true。问题是,在同一帧中,每次我把它设为真时,它似乎都会重置为假。我把bool变成了一个属性,并在set方法中设置了一个断点,它只会以"true"作为参数被调用。
是我做错了什么,还是Unity做了一些奇怪的东西?
private bool SceneMoveUp
{
get
{
return _sceneMoveUp;
}
set
{
_sceneMoveUp = value;
}
}
void Update () {
if (SceneMoveUp == true) {
transform.position = Vector3.MoveTowards(transform.position, SceneDestination, speed * Time.deltaTime);
}
}
编辑:SceneMoveUp是monobehaviour派生类中的一个属性。
EDIT2:完整代码
using UnityEngine;
using System.Collections;
public class PseudoScene : MonoBehaviour {
public Vector3 ZoomOutPosition;
public GameObject[] Letters;
public Vector3[] CameraPositions;
public PseudoSceneManager _manager;
public float speed;
private float DepartureTime;
private Vector3 TextureOffset;
private int LetterCount, CurrentIndex = 0;
private bool ZoomingOut = false;
private bool _sceneMoveUp;
private bool SceneMoveUp{ get
{
return _sceneMoveUp;
}
set
{
_sceneMoveUp = value;
}
}
static public bool MovingCamera = false;
private Vector3 BackgroundFinalScale;
private Vector3 SceneDestination;
[SerializeField]private Quaternion FinalRotation;
void Start () {
LetterCount = Letters.Length;
SceneMoveUp = false;
if (LetterCount == 1) {
CameraPositions = new Vector3[1];
CameraPositions[0] = transform.position;
CameraPositions[0].z = -10;
}
ChangeLetter();
}
public void LetterFilled () {
CurrentIndex++;
if (CurrentIndex < LetterCount) {
ChangeLetter();
} else {
if (LetterCount > 1) {
ZoomOut();
} else {
MoveScene();
}
Invoke("FinishScene", 3f);
}
}
void Update () {
if (ZoomingOut) {
Camera.main.transform.position = Vector3.Lerp(transform.position, ZoomOutPosition, 1f);
Camera.main.orthographicSize = Mathf.Lerp(Camera.main.orthographicSize, 13, 0.1f);
}
if (MovingCamera) {
Camera.main.transform.position = Vector3.MoveTowards(Camera.main.transform.position, CameraPositions[CurrentIndex], speed * Time.deltaTime);
if (Camera.main.transform.position == CameraPositions[CurrentIndex]) {
MovingCamera = false;
Checkpoint.DeactivateAllCheckpoints(true);
if (LetterCount > 1 && CurrentIndex >= 1) {
Checkpoint.DeactivateAllCheckpoints(true);
}
}
}
if (SceneMoveUp == true) {
transform.position = Vector3.MoveTowards(transform.position, SceneDestination, speed * Time.deltaTime);
}
}
void MoveScene () {
DepartureTime = Time.time;
SceneMoveUp = true;
Debug.logger.Log("set SceneMoveUp to ", SceneMoveUp.ToString());
SceneDestination = transform.position;
SceneDestination.y += 10;
}
public void FinishScene () {
_manager.SceneFinished();
}
//5.75
void ChangeLetter () {
Checkpoint.DeactivateAllCheckpoints(false);
MovingCamera = true;
}
void ZoomOut () {
ZoomingOut = true;
}
}
似乎有一个随机的错误,使它不能工作。我删除了这个类,用完全相同的代码创建了一个新的类(复制粘贴),现在它可以工作了。谢谢大家的努力。
这听起来很疯狂,我也遇到了同样的问题。经过几个小时的调试,我将代码复制粘贴到一个新类中。砰!它工作。我假设我做了一些导致错误的事情,并希望我知道它是什么。但是复制粘贴成功了。