I need help, I have a code for generating a level, each time the generation happens randomly, so I can't make it so that when the trigger is reached on a certain section of the level, at the end of the level on the last platform, more platforms were generated, I tried to search for an object through pos = GameObject.Find("Platform 4 End Platform(Clone)").transform.position; , but in the end I did not come to success,can someone explain how to do it best? All the code is attached below, as I have a generation, etc.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class LevelGeneratorRock : MonoBehaviour
{
[SerializeField]
private GameObject startPlatform, endPlatform, platformPrefab, triggerPlatform;
private float blockWidth = 0.5f, blockHeight = 0.2f;
[SerializeField]
private int amountToSpawn = 100;
private int beginAmount = 0;
public Vector3 pos;
private List<GameObject> spawnedPlatforms = new List<GameObject>();
[SerializeField]
private GameObject playerPrefab;
void Awake()
{
beginAmount = 0;
amountToSpawn = 10;
InstantiateLevel();
}
const int MAX_PLATFORMS = 100;
void InstantiateLevel()
{
// Notice the conditional changed here
for (int i = beginAmount; i < beginAmount + amountToSpawn; i++)
{
GameObject newPlatform;
if (i == 0)
{
newPlatform = Instantiate(startPlatform);
}
else if (i == MAX_PLATFORMS)
{ // End Platform
newPlatform = Instantiate(endPlatform);
newPlatform.tag = "EndPlatform";
}
else if (i % 10 == 0)
{
newPlatform = Instantiate(triggerPlatform);
newPlatform.tag = "triggerPlatform";
}
else
{ // Normal platform
newPlatform = Instantiate(platformPrefab);
}
spawnedPlatforms.Add(newPlatform);
int left = Random.Range(0, 2);
if (left == 0)
{
newPlatform.transform.position =
new Vector3(pos.x - blockWidth, pos.y + blockHeight, pos.z);
}
else
{
newPlatform.transform.position =
new Vector3(pos.x, pos.y + blockHeight, pos.z + blockWidth);
}
pos = newPlatform.transform.position;
// fancy animation
if (i < 25)
{
float pos = newPlatform.transform.position.y;
newPlatform.transform.position =
new Vector3(newPlatform.transform.position.x,
newPlatform.transform.position.y - blockHeight * 3f,
newPlatform.transform.position.z);
newPlatform.transform.DOLocalMoveY(pos, 0.3f).SetDelay(i * 0.1f);
}
} // for loop
} // instantiate level
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("triggerPlatform"))
{
// Tell the level generator to spawn more platforms
{
beginAmount = beginAmount + 11;
amountToSpawn = 10;
InstantiateLevel();
}
}
}
}// class
