如何在Unity 5中将float从一个脚本传递到另一个脚本? C#

时间:2015-05-06 10:02:17

标签: c# unity3d

我在Unity 5个人版中工作..我试图将float从一个脚本传递到另一个脚本,但是我收到了这个错误:

  

Assets / CameraTracksPlayer.cs(15,36):错误CS1061:输入   ClimberMovement' does not contain a definition for stopCamera'和   没有扩展方法stopCamera' of type ClimberMovement'可能   发现(您是否缺少using指令或程序集引用?)

我不知道自己做错了什么。

这是我的代码:

using UnityEngine;
using System.Collections;

public class CameraTracksPlayer : MonoBehaviour {

    Transform player;
    float isDead;
    float offsetY;

    // Use this for initialization
    void Start () {

        GameObject go = GameObject.Find ("MainCamera");
        ClimberMovement dying = go.GetComponent <ClimberMovement> ();
        float died = dying.stopCamera; //this line gets error!
        isDead = died;


        GameObject player_go = GameObject.FindGameObjectsWithTag("Player")[0];

        if (player_go == null) {
            Debug.LogError("Ne dela ker ni taga Player");
            return;
        }
        player = player_go.transform;

        offsetY = transform.position.y - player.position.y;

    }

    // Update is called once per frame
    void Update () {
        if (player != null && isDead == 1) {
            Vector3 pos = transform.position;
        }
        else if (player != null) {
            Vector3 pos = transform.position;
            pos.y = player.position.y + offsetY;
            transform.position = pos;
        }

    }

}

这是我尝试传递变量的脚本代码:

using UnityEngine;
using System.Collections;

public class ClimberMovement : MonoBehaviour {

    Vector3 velocity = Vector3.zero;
    public Vector3 gravity;
    public Vector3 climbVelocity;
    public Vector3 climbVelocityJump;
    public float maxSpeed = 5f;
    public bool stopCamera = false;

    bool didClimb = false;
    bool didJump = false;
    bool jumping = false;
    bool jumping2 = false;

    Animator animator;

    bool dead = false;

    // Use this for initialization
    void Start () {
        animator = transform.GetComponentInChildren<Animator> ();
        animator.SetTrigger("DoClimb");
    }

    //do gfx input update here
    void Update () {

        if(Input.GetMouseButtonDown(0)){
            didClimb = true;
            //didJump = true;
        }
        if (Input.GetMouseButtonUp(0)) {
            jumping2 = true;
        }
    }

    // do physics engine update here
    void FixedUpdate () {

        velocity += gravity * Time.deltaTime;

        if(didClimb == true){
            didClimb = false;
            jumping = true;
            velocity += climbVelocity;
        }

        else if (jumping2 == true && dead == false) {
            jumping = false;
            jumping2 = false;
            velocity += climbVelocityJump;
        }

        velocity = Vector3.ClampMagnitude(velocity, maxSpeed);

        transform.position += velocity * Time.deltaTime;

        if (transform.position.x <= -3.9f) {
            transform.position = new Vector3 (-3.9f, transform.position.y, transform.position.z);
            if (transform.position.x == -3.9f) {
                dead = true;
                stopCamera = true;
                animator.SetTrigger ("Death");
                velocity.y = -5f;
                velocity.x = -5f;
            }
        } else if (transform.position.x >= -1f) {
            transform.position = new Vector3 (-1f, transform.position.y, transform.position.z);
        }


    }

}

2 个答案:

答案 0 :(得分:2)

1 - 您想在Converter中投射public bool stopCamera = false;

尝试使用float died

更改float died

2 - 如果您有NullReferenceException,则表示它未在您的&#34; MainCamera&#34;中找到您的脚本。游戏对象。

尝试添加您的脚本&#34; ClimberMovement&#34;在你的&#34; MainCamera&#34;游戏对象。

希望它可以帮到你!

答案 1 :(得分:-2)

我对你的案例的建议是有一个返回该值的方法:

public float stopCameraValue(){
return stopCamera;
}

我猜测monobehaviour阻止你直接访问该变量,因此出错。由于您没有在CameraTracksPlayer类中设置ClimberMovement类,因此可能认为它在编译时没有实例化。