一个变量,用于将此类中的值保存到unity3d中的另一个类中?

时间:2017-05-11 19:05:29

标签: c# reference unity5

大家好我有一个Segment类,它保存了一个名为nextSegment的值。代码如下。

public class Segment : MonoBehaviour {

    public SplineComputer spline;
    public Segment nextSegment;

    // Use this for initialization
    void Start () {

        spline = GetComponentInChildren<SplineComputer>();
        nextSegment = GetComponent<Segment>();

        if (nextSegment == null)
            Debug.Log("No segement");
        else
            Debug.Log("present "+ nextSegment);

        spline.space = SplineComputer.Space.Local;
        SplinePoint[] points = spline.GetPoints();

        if (points.Length == 0)
            return; 
    }

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

    }
}

在这个类中,我有一个名为currentSegmant的变量。它写成如下。

public class Player :MonoBehaviour {
void Start()
    {
        controller = GetComponent<CharacterController>();
        anim = GetComponent<Animator>();
        controller = GetComponent<CharacterController>();
        follower = GetComponent<SplineFollower>();

        currentSegment = Segment.nextSegment; //Error 

        Debug.Log(currentSegment);

        follower.onEndReached += Follower_onEndReached;

        currentDistanceOnPath = 50f;

    }

    private void Follower_onEndReached()
    {
        currentSegment = currentSegment.nextSegment;
        follower.computer = currentSegment.spline;

        Debug.Log(follower.computer);
    }
}

如上所示,我想在Player类nextSegment变量中保存Segment类currentSegment变量。我该如何实现这一目标?我上面实现的代码给出了一个错误,我将其显示为Error,评论。

供参考错误是:非静态字段,方法或属性需要对象引用&#39; Segment.nextsegment&#39;

0 个答案:

没有答案