Skip to main content
Post Closed as "Needs details or clarity" by DMGregory, Vaillancourt, Gnemlock, CommunityBot, Kromster
Changed title to be more generic, so others with same problem can find it easier
Link
Vaillancourt
  • 16.4k
  • 17
  • 56
  • 61

How to call a public function from another script in Unity?

Changed title to be more generic, so others with same problem can find it easier
Link

How to Call SetPointscall public function from another classscript in Unity

Source Link
Nihal Saxena
  • 103
  • 1
  • 1
  • 3

How to Call SetPoints function from another class

public class MoveTowards : MonoBehaviour {

    int currentStartPoint;
    private Vector3 startMarker, endMarker;
    public float speed = 1.0F;
    private float startTime;
    private float journeyLength;
    public float smooth = 5.0F;
    List<Vector3> tempPositionList = new List<Vector3>();
    List<Vector3> positionList = new List<Vector3>
        {
            new Vector3(4,0,0),
            new Vector3(4,0,2),
            new Vector3(4,0,4),
            new Vector3(2,0,4),
            new Vector3(0,0,4),
            new Vector3(-2,0,4),
            new Vector3(-4,0,4),
            new Vector3(-4,0,2),
            new Vector3(-4,0,0),
            new Vector3(-4,0,-2),
            new Vector3(-4,0,-4),
            new Vector3(-2,-0,-4),
            new Vector3(0,0,-4),
            new Vector3(2,0,-4),
            new Vector3(4,0,-4),
            new Vector3(4,0,-2),
            new Vector3(2,0,-2),
            new Vector3(0,0,-2),
            new Vector3(-2,0,-2),
            new Vector3(-2,0,0),
            new Vector3(-2,0,2),
            new Vector3(0,0,2),
            new Vector3(2,0,2),
            new Vector3(2,0,0),
            new Vector3(0,0,0)
        };
    void Start()
    {
        currentStartPoint = 0;
        SetPoints();
    }
    public void SetPoints()
    {
        startMarker = positionList[currentStartPoint];
        endMarker = positionList[currentStartPoint + 1];
        startTime = Time.time;
        journeyLength = 2;
    }

    void Update()
    {
        Debug.Log(positionList.Count);
        float distCovered = (Time.time - startTime) * speed;
        float fracJourney = distCovered / journeyLength;
        transform.position = Vector3.Lerp(positionList[currentStartPoint], positionList[currentStartPoint + 1], fracJourney);
        if (fracJourney >= 1f && currentStartPoint + 1 < positionList.Count)
        {
            currentStartPoint++;
            SetPoints();
        }
    }

}