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

在Unity 5中,如何将float从一个脚本传递到另一个脚本?C#,c#,unity3d,C#,Unity3d,我在Unity 5个人版工作。。我试图将float从一个脚本传递到另一个脚本,但出现以下错误: Assets/CameraTracksPlayer.cs(15,36):错误CS1061:类型 ClimberMovement'不包含对stopCamera'和 无法使用类型为攀登运动”的扩展方法 找到(是否缺少using指令或程序集引用?) 我不知道我做错了什么 这是我的密码: using UnityEngine; using System.Collections; public class Ca

我在Unity 5个人版工作。。我试图将float从一个脚本传递到另一个脚本,但出现以下错误:

Assets/CameraTracksPlayer.cs(15,36):错误CS1061:类型
ClimberMovement'不包含对
stopCamera'和 无法使用类型为攀登运动”的扩展方法 找到(是否缺少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;
        }

    }

}
使用UnityEngine;
使用系统集合;
公共类CameraTracksPlayer:单行为{
转换玩家;
浮子是死的;
浮动偏置;
//用于初始化
无效开始(){
GameObject go=GameObject.Find(“主摄像头”);
爬升运动=go.GetComponent();
float died=diing.stopCamera;//此行出错!
isDead=死亡;
GameObject player_go=GameObject.FindGameObjectsWithTag(“玩家”)[0];
if(player_go==null){
LogError(“Ne dela ker ni taga播放器”);
回来
}
player=player\u go.transform;
offsetY=transform.position.y-player.position.y;
}
//每帧调用一次更新
无效更新(){
if(player!=null&&isDead==1){
向量3位置=变换位置;
}
else if(player!=null){
向量3位置=变换位置;
pos.y=播放器位置.y+偏移;
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);
        }


    }

}
使用UnityEngine;
使用系统集合;
公共类攀爬运动:单一行为{
矢量3速度=矢量3.0;
公共矢量3引力;
公共向量3;
公共矢量3爬升速度跳跃;
公共浮点数maxSpeed=5f;
公共bool stopCamera=假;
bool-didlimp=false;
bool didJump=false;
布尔跳跃=错误;
bool跳线2=错误;
动画师;
布尔死=假;
//用于初始化
无效开始(){
animator=transform.getComponentChildren();
设置触发器(“DoClimb”);
}
//这里有gfx输入更新吗
无效更新(){
if(Input.GetMouseButtonDown(0)){
didlimp=正确;
//didJump=true;
}
if(Input.GetMouseButtonUp(0)){
跳跃2=真;
}
}
//这里有物理引擎更新吗
无效固定更新(){
速度+=重力*时间增量;
如果(didGramb==真){
didlimp=false;
跳跃=正确;
速度+=上升速度;
}
else if(jumping2==true&&dead==false){
跳跃=错误;
跳跃2=错误;
速度+=爬升速度跳跃;
}
速度=矢量3.夹紧幅度(速度,最大速度);
transform.position+=速度*时间增量;
if(transform.position.x=-1f){
transform.position=新矢量3(-1f,transform.position.y,transform.position.z);
}
}
}

1-您想要播放
public bool stopCamera=false
float-died

尝试使用
bool died

2-如果你有一个NullReferenceException,这意味着它在你的“MainCamera”游戏对象中找不到你的脚本

尝试在“MainCamera”游戏对象中添加脚本“攀登运动”


希望它能帮助你

我对您案例的建议是使用一个返回该值的方法:

public float stopCameraValue(){
return stopCamera;
}

我想,MonoBehavior会阻止您直接访问该变量,从而产生错误。由于您没有在CameraTracksPlayer类中安装ClimberMovement类,它可能会认为它没有在编译时安装。

奇怪的是,此代码的当前形式应该可以工作。你确定你复制了它的原样吗?另外,请不要在没有阅读标签描述的情况下使用标签
unity
标记与unity游戏引擎无关。顺便说一下,您似乎使用
isDead
of
float
类型来保存布尔值。C#(以及几乎所有其他现代语言)有一种特殊类型,即
bool
。只需在我自己的unity场景中测试您的代码,就可以了。你能给我们你的整个攀登者动作脚本吗?@Fisheroid如果脚本不在gameObject中,你应该在getComponent行有一个nullReferenceException