Home > Enterprise >  When counting last visited waypoints using ui text, how to make it to start counting from 1 instead
When counting last visited waypoints using ui text, how to make it to start counting from 1 instead

Time:04-04

The goal is to count from 1 and from 0.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class MoveOnCurvedLines : MonoBehaviour
{
    public LineRenderer lineRenderer;
    public float speed;
    public bool go = false;
    public bool moveToFirstPositionOnStart = false;
    public float rotSpeed;
    public bool random = false;
    public int currentCurvedLinePointIndex;
    public Text lastWaypointText;

    private Vector3[] positions;
    private Vector3[] pos;
    private int index = 0;
    private bool goForward = true;
    private List<GameObject> curvedLinePoints = new List<GameObject>();
    private int numofposbetweenpoints;
    private bool getPositions = false;
    int randomIndex;
    int curvedPointsIndex;

    // Start is called before the first frame update
    void Start()
    {
        curvedLinePoints = GameObject.FindGameObjectsWithTag("Curved Line Point").ToList();

        if (curvedLinePoints != null && curvedLinePoints.Count > 0)
        {
            transform.rotation = curvedLinePoints[1].transform.rotation;
        }

        if (random)
            GetNewRandomIndex();

        lastWaypointText.text = curvedPointsIndex   1.ToString();
    }

    Vector3[] GetLinePointsInWorldSpace()
    {
        positions = new Vector3[lineRenderer.positionCount];
        //Get the positions which are shown in the inspector 
        lineRenderer.GetPositions(positions);


        //the points returned are in world space
        return positions;
    }

    // Update is called once per frame
    void Update()
    {
        if (lineRenderer.positionCount > 0 && getPositions == false)
        {
            pos = GetLinePointsInWorldSpace();
            numofposbetweenpoints = pos.Length / curvedLinePoints.Count;

            if (moveToFirstPositionOnStart == true)
            {
                transform.position = pos[index];
            }

            getPositions = true;
        }

        if (go == true && lineRenderer.positionCount > 0)
        {
            Move();

            Vector3 targetDirection = (curvedLinePoints[c].transform.position - transform.position).normalized;
            curvedLinePoints[c].transform.localRotation = Quaternion.LookRotation(targetDirection);
            transform.localRotation = Quaternion.RotateTowards(transform.localRotation, curvedLinePoints[c].transform.localRotation, Time.deltaTime * rotSpeed);
        }

        var dist = Vector3.Distance(transform.position, curvedLinePoints[curvedPointsIndex].transform.position);
        if (dist < 0.1f)
        {
            if (curvedPointsIndex < curvedLinePoints.Count - 1)
            {
                curvedPointsIndex  ;

                lastWaypointText.text = curvedPointsIndex.ToString();
            }

            currentCurvedLinePointIndex = curvedPointsIndex;
        }
    }

curvedPointsIndex in the Start() is 0 so if there are 30 curvedLinePoints it will start counting from 0 to 29 but i want it to display in the lastWaypointText.text from 1 to 30.

I tried in the Start() instead :

lastWaypointText.text = curvedPointsIndex.ToString();

To make

lastWaypointText.text = curvedPointsIndex   1.ToString();

but that's making the first number in the text 01 and then counting to 29.

CodePudding user response:

When you do lastWaypointText.text = curvedPointsIndex 1.ToString(); you are just applying ToString to the last part of the formula which makes it add "1" after your lastWaypointIndex. You need to wrap it in a parenthesis instead like this:

lastWaypointText.text = (curvedPointsIndex 1).ToString();

  • Related