How do you generate a line between objects that are only instantiated upon game Start ?

Hi Community, I believe there are many solutions out there addressing how to draw a line with your mouse or assigning existing objects to dependencies like generating a line, which is however not what I am looking for.

More importantly, how do you generate a line between objects that don’t exist(prior to starting the game) ? I am currently doing a project using MapBox SDK, and my ‘waypoints’ (or objects) are only created upon starting the game.

Haven’t been able to find any threads on this, thank you in advance :slight_smile:

Easiest way would be to assign a LineRenderer component to a GameObject (can be a new one or one of the objects you want to draw the line on), and then in script assign the positions of the LineRenderer component like so:

using UnityEngine;

public class DrawLineBetweenTwoObjects : MonoBehaviour
{
    public GameObject objectOne;    // The first object you instantiate
    public GameObject objectTwo;    // The second object you instantiate

    public LineRenderer lineRend;   // The linerenderer component, remember to assign this in the inspector!

    void Start()
    {
        // Set the position count of the linerenderer to two
        lineRend.positionCount = 2;

        // Instantiate the two objects
        objectOne = Instantiate(*yourFirstInstantiatedPrefabHere*, *yourPositionHere*, Quaternion.identity);
        objectTwo = Instantiate(*yourSecondInstantiatedPrefabHere*, *yourPositionHere*, Quaternion.identity);

        // Get the transform of the two objects
        Transform first = objectOne.transform;
        Transform second = objectTwo.transform;

        DrawLineBetweenObjects(first, second);
    }

    void DrawLineBetweenObjects (Transform firstT, Transform secondT)
    {
        // Set the positions of the LineRenderer
        lineRend.SetPosition(0, firstT.position);
        lineRend.SetPosition(1, secondT.position);
    }
}