如果变量上升或下降,则使对象移动

时间:2018-02-15 13:49:46

标签: c# unity3d

我试图建立一个变量控制对象位置的系统,这样如果变量上升,那么对象的Y位置会上升,如果变量变低,那么对象的Y位置将会走了。

我试图从单独的脚本控制变量,但变量的值不会改变。

变量脚本:

public class Money : MonoBehaviour {
public static float Flow;
public static float FlowPos;
    // Use this for initialization
    void Start () {
        Flow = 50;

}

// Update is called once per frame
void Update () {
    FlowPos = Flow/100;
    print(Flow);
    transform.position = new Vector3(10.56f,12 + FlowPos,1);
}

尝试更改变量的脚本:

using UnityEngine;
using System.Collections;

public class Barista01 : MonoBehaviour {

bool dragging = false;
bool CardActive = true;

void Start(){





}

void Update(){  

    if (transform.position.x > 11.5f && dragging == false){

        GameControllerScript.Active = false;
        this.Destroy(gameObject);
    }
    else if (transform.position.x < 4.5f && dragging == false){

        GameControllerScript.Active = false;
        Worker.Morale += 20;
        Money.Flow -= 15;
        this.Destroy(gameObject);

    }

    if (GameControllerScript.CardCall == 2 && CardActive == true){
        CardActive = false;
        Worker.Morale -= 20;
        transform.position = new Vector3(8f, 6.89f, 9f);

    }

}

void OnMouseDown(){
    {
        dragging = true;

    }
}

void OnMouseUp(){
    {
        dragging = false;

    }
}

}

编辑:我将加号,减号和eaquals符号放在正确的位置但是变量仍然没有改变

1 个答案:

答案 0 :(得分:1)

你应该纠正这两行:

Worker.Morale =+ 20;
Money.Flow =- 15;

Worker.Morale += 20;
Money.Flow -= 15;