1
votes

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

}

2
Well, in this line if(SceneMoveUp == true) you're just checking if the Property SceneMoveUp is true. It's the same as if(SceneMoveUp). We're missing the point, were you actually setting the value of SceneMoveUpChristoph K
Use ViewState to prevent the valueTechidiot
Fair enough. I had: 'if (SceneMoveUp)' before, but I turned paranoid and added == true just to be safe.Mihai Stan
@Techidiot what do you mean?Mihai Stan

2 Answers

1
votes

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.

-2
votes

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.