简体   繁体   中英

Unity C# bool resetting to false

I have a boolean variable which I set to true when I want certain parts of my scene to move up. The problem is, it seems to be resetting to false every time I set it to true, during the same frame. I've made the bool into a property and set a breakpoint in the set method and it only ever gets called with "true" as a parameter.

Is there something I'm doing wrong, or is Unity doing some weird stuff behind?

private bool SceneMoveUp
{   
    get
    {
        return _sceneMoveUp;
    }
    set
    {
        _sceneMoveUp = value;    
    }
}

void Update () {
    if (SceneMoveUp == true) {
        transform.position = Vector3.MoveTowards(transform.position, SceneDestination, speed * Time.deltaTime);
    }
}

EDIT: SceneMoveUp is a property inside a MonoBehaviour-derived class.

EDIT2: entire code

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;
}

}

It seems there was a random bug which made it not work. I deleted the class, created a new one with the exact same code (copy-paste) and it now works. Thanks everyone for trying.

As crazy as this sounds, I had the exact same problem. After hours of trying to debug, I copy-pasted the exact code into a new class. Bam! It worked. I assume that I did something to cause the error and wish I new what it was. But copy-paste worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM