For example let's say the depthLevel value is 2 and then in objectsList there is 76 items.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class ObjectList : MonoBehaviour
{
[Range(0, 10)]
public int depthLevel = 0;
public GameObject targetObject;
public List<Transform> objectsList = new List<Transform>();
private Transform targetTransform;
private void Start()
{
targetTransform = targetObject != null ? targetObject.transform : transform;
GetNestedChildren(targetTransform, depthLevel);
}
private void Update()
{
if (depthLevel != objectsList.Count - 1)
{
objectsList.Clear();
GetNestedChildren(targetTransform, depthLevel);
}
}
private void GetNestedChildren(Transform parent, int depth)
{
if (depth <= 0)
{
objectsList.Add(parent);
return;
}
for (int i = 0; i < parent.childCount; i++)
{
Transform child = parent.GetChild(i);
objectsList.Add(child);
GetNestedChildren(child, depth - 1);
}
}
}
Now I want to get the objectsList variable and use it in this script as waypoints:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlyCamera : MonoBehaviour
{
public Transform[] waypoints;
public float movementSpeed = 2f;
public float minDistance = 1f;
public float maxDistance = 10f;
public float minHeight = 0f;
public float maxHeight = 10f;
public bool randomizeVisitOrder = false;
public ObjectList objectList;
private int currentWaypointIndex = 0;
private bool isMoving = false;
private void Start()
{
waypoints = GameObject.FindGameObjectWithTag("Waypoint").GetComponentsInChildren<Transform>();
if (waypoints.Length > 0)
{
transform.position = waypoints[0].position;
}
StartMovement();
}
private void Update()
{
if (isMoving)
{
MoveToWaypoint();
}
}
private void MoveToWaypoint()
{
if (currentWaypointIndex >= waypoints.Length)
{
// Reached the last waypoint, stop moving
isMoving = false;
return;
}
Vector3 targetPosition = waypoints[currentWaypointIndex].position;
targetPosition += RandomOffset();
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * movementSpeed);
// Check if we are close enough to the current waypoint
if (Vector3.Distance(transform.position, targetPosition) < minDistance)
{
currentWaypointIndex++;
if (randomizeVisitOrder && currentWaypointIndex < waypoints.Length)
{
// Randomize the next waypoint index
int randomIndex = Random.Range(currentWaypointIndex, waypoints.Length);
Transform temp = waypoints[currentWaypointIndex];
waypoints[currentWaypointIndex] = waypoints[randomIndex];
waypoints[randomIndex] = temp;
}
}
}
public void StartMovement()
{
if (waypoints.Length > 0)
{
currentWaypointIndex = 0;
isMoving = true;
}
}
private Vector3 RandomOffset()
{
float distance = Random.Range(minDistance, maxDistance);
float angle = Random.Range(0f, 360f);
float height = Random.Range(minHeight, maxHeight);
Vector3 offset = new Vector3(distance * Mathf.Cos(angle * Mathf.Deg2Rad), height, distance * Mathf.Sin(angle * Mathf.Deg2Rad));
return offset;
}
}
The problem is in the Start of the FlyCamera script:
waypoints = objectList.objectsList;
objectsList is still null. how can I wait first the objectsList to be get all objects first and then using it in the FlyCamera script?