无法在不同的脚本中访问变量:Unity C#

时间:2014-11-07 18:58:38

标签: c# unity3d

目前我正在使用C#编写的第一个视频游戏,但现在我遇到了麻烦,我不知道该怎么做了。首先我已经完成了我的角色移动但是,我想把所有属性放在一个脚本中,比如它的moveSpeed,attackSpeed等等,但是一旦我将它访问到另一个脚本,我的角色就​​站着什么都不做。这是我的代码

public class ClickToMove : MonoBehaviour {
    public CharacterController controller;          // Use to move the player
    private Vector3 position;                       // Store the position at which the player clicked;
    public Attributes attribute;

    // Animation variables
    public AnimationClip idleClip;
    public AnimationClip runClip;

    // Use this for initialization
    void Start () {
        position = transform.position;          // Set the position to player's current position
    }

    // Update is called once per frame
    void Update () {
        // Execute the code below once the player press left click
        if(Input.GetMouseButton(0)) {
            locatePosition();
        }
        moveToPosition();
    }

    // Locate at which the player clicked on the terrain
    private void locatePosition() {
        RaycastHit hit;                                                     // Get information from ray
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);        // A line in 3D space

        // Cast a ray that start from the camera with a distance of 1000
        if(Physics.Raycast(ray, out hit, 1000)) {
            // Store the position if the casted ray is not pointing to the player or enemy's position
            if(hit.collider.tag != "Player" && hit.collider.tag != "Enemy") {
                position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
            }
        }
    }

    // Move to the located position
    private void moveToPosition() {
        // Check if the player position and the destination is greater than one
        if(Vector3.Distance(transform.position, position) > 1) {
            // Subtract the clicked position to the player position
            Quaternion newRotation = Quaternion.LookRotation (position - transform.position);
            // Disable the rotation from x and z angle
            newRotation.x = 0f;
            newRotation.z = 0f;
            // Rotate the player to a new rotation then move forward
            transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * attribute.moveSpeed);
            controller.SimpleMove(transform.forward * attribute.moveSpeed);
            animation.CrossFade(runClip.name);
        } else {
            animation.CrossFade(idleClip.name);
        }
    }
}

这是我正在访问的脚本

public class Attributes : MonoBehaviour {
    public float moveSpeed;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }
}

我不知道该怎么办了。任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

有两件事你需要仔细检查,第一件事是你实际上在检查员中为attribute.moveSpeed分配了一个值。否则moveSpeed始终为0,因此所有乘法运算都会产生0,导致根本无法移动。

其次,确保您的光线投射实际上正在击中某些内容。您可以通过几种方式执行此操作,首先将一条消息打印到控制台,说明您遇到了什么

// Cast a ray that start from the camera with a distance of 1000
if(Physics.Raycast(ray, out hit, 1000)) {

    // Send a debug message to Unity's console
    Debug.Log("I hit Something!");        

    // Store the position if the casted ray is not pointing to the player or enemy's position
    if(hit.collider.tag != "Player" && hit.collider.tag != "Enemy") {
        position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
    }
}

,然后检查position变量是否在检查器中更改了值。 (公开position变量并查看角色的检查员)

请注意,您已在检查器中为attribute变量分配了一个值,因为您未获得NullReferenceExceptions