Is there a way to call on the highest value of an integer that has been set?

To get straight to the point, I’m a noob.
Trying to learn both Unity and C# at the same time.
Working on a code that will create infinite endless runner by using my prefabs as the ground.
I was thinking of using an emptyObject at the end of each prefab that will be the mark for where the new prefab should start. I’ve googled the whole day, periodically of course, but cannot find the right call or the right way to instantiate the prefab at the end of anotherone unless they are all completely the same size.

heres the code I jumbled up together

public class InfinitePlanes : MonoBehaviour
{
    public Transform endPos;
    public int lastPos;
    public GameObject[] RoadPrefabs;
    public int objectsSpawned = 0;


    
     private void Update()
    {
        //if //Help (lastPos choose highest int) OR should I

        objectsSpawned = objectsSpawned + 1;

        Instantiate(RoadPrefabs[Random.Range(0, RoadPrefabs.Length)], new Vector3 //HELP endPos.position.x, startPos.position.y, transform.position.z) Quaternion.Identity;

    }
}

Should I scrap the way Im trying to do this? Im trying to refrain from having every prefab be the same size.
Thanks in advance :slight_smile:

also, I know this will run on forever. I thought once I have the tiles spawning the right way I can limit it to spawning only a handful at a time, with a collisionzone that will deactivate the prefab after the player left it after a set number of seconds. Im pretty sure I know how to do that. Ive done something similar before.

Yeah the whole destroying thing isn’t that hard, just use the distance from the player, a world bubble like you use with bullets so that they automatically destroy with distance. So! I would have 2 ways of doing this, depending on if you have curves or not. If the road just goes straight, you can place your new piece just by knowing the previous object. If you take the position of the last placed object, add half of the objects size by using .bounds or .extends or something, than add half of your new object by the same method, and there is your new position.


If you have curves, this is not really a good method. You could simply create a script for your prefabs where you can enter an exit and entry position, or even use a empty game object to move around if that is what you prefer and get the position of those in your script. It will be a little bit of tweaking of course because your want them to line up properly, or you could have a little bit of overlap of course. With the information you get from the 2 points and the position of the object, you could use methods like transform.TransformPoint and transform.TransformDirection to move around your pieces.