简体   繁体   中英

A variable to hold value in this class to from another class in unity3d?

Hi all I have a Segment class which hold this value called nextSegment The code is as follows.

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 () {

    }
}

In this class I have a variable called currentSegmant. It is written as follows.

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);
    }
}

As seen above I want to hold the Segment class nextSegment variable in Player class currentSegment variable. How do I achieve this? The code I have implemented above gives an error which I have shown as Error, commented.

For reference The error is: an object reference is required for non-static field, method or property 'Segment.nextsegment' .

This error is because you don't declare the Segment class in the player class or Segment is not an static class.

there are a few ways to solve this problem:

Option 1: Add the following lines to your player class (above the currentSegment = Segment.nextSegment; //Error line):

[SerializeField]
private Segment segment

When you added this 2 lines change this line:

currentSegment = Segment.nextSegment; //Error 

To:

currentSegment = segment.nextSegment;

And finally drag the GameObject (where the Segment script is attached to) to the serializable field in the inspector(when you have selected the GameObject where the player script is attached to).

Option2: You can make the Segment class Static.

change this line:

public class Segment : MonoBehaviour {

To:

public static class Segment : MonoBehaviour{

than add the following line(beneath the class header):

public Segment instace;

void Awake(){
instance = this;
}

now change:

currentSegment = Segment.nextSegment; //Error 

to:

currentSegment = Segment.instance.nextSegment;

The first option I gave you is in my opinion the best solution to fix this problem.

I hope this answer can help you solve this error.

Tom,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM