Choose Waypoints from a Random Array of Paths
Hello actually i have a Problem about a solution to have a Array with Paths, its a GameObject in Scene and contains a various amount of Waypoints, every Scene will have different amount of Paths and Waypoints. The Script is Attached to the NPC.
Now i want spawn Random Enemys on the Scene and they should choose randomly a Path and follow the Waypoints in this Path
You can see actually 3 Paths and every Path contains 3 Waypoints. He load all Paths and then he Choose a Path with index (on Screen it is Path 2 (3))
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace enemy
{
public class NPC_Waypoint : MonoBehaviour
{
//Paths
[SerializeField] GameObject[] paths;
[SerializeField] int index;
//Waypoint
[SerializeField] GameObject[] wayPoints;
//for give the State Machine the Waypoints
private NPC_State_Machine npc;
//Looking for all Paths in Scene and get the Waypoints out of the Paths
public void Awake()
{
//Load the Paths
paths = GameObject.FindGameObjectsWithTag("Paths");
//Check if Paths not null
if (paths != null)
foreach (GameObject path in paths)
{
//Show All Paths
Debug.Log("Path: " + path);
}
else
//Do Something else
Debug.Log("Have No Paths");
}
private void Start()
{
//Start to Choose a Random Path and load the Waypoints in the Path
Debug.Log("Choose a Path");
RandomPath();
}
public void RandomPath()
{
index = Random.Range(0, paths.Length);
//Get Random Index from Paths and load the Waypoints from it
Debug.Log("Path " + index + " choosen");
//Im Not sure how i can tell him to get the Waypoints out of the Paths
//here i try to get the Waypoints from each Path
wayPoints = gameObject.GetComponentsInChildren<GameObject>(paths[index]);
if (wayPoints != null)
foreach (GameObject way in wayPoints)
{ Debug.Log("Waypoint: " + way); }
else
Debug.Log("Path have no WayPoints");
}
}
}
ArgumentException: GetComponent requires that the requested component 'GameObject' derives from MonoBehaviour or Component or is an interface.
UnityEngine.GameObject.GetComponentsInChildren[GameObject] (Boolean includeInactive) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/GameObjectBindings.gen.cs:142)
enemy.NPC_Waypoint.RandomPath () (at Assets/Scripts/Enemy/EnemyAI/NPC_Waypoint.cs:64)
enemy.NPC_Waypoint.Start () (at Assets/Scripts/Enemy/EnemyAI/NPC_Waypoint.cs:43)
So he not load the Waypoints. Someone have an Idea how i can load the Waypoints from each Path?
I Hope i was able to explain whats my goal and someone have an idea how to fix this problem.
Actually i fix the $$anonymous$$ain Problem
i change
[SerializeField] GameObject[] wayPoints;
to
[SerializeField] Transform[] wayPoints;
and
wayPoints = gameObject.GetComponentsInChildren(paths[index]);
to
wayPoints = paths[index].GetComponentsInChildren<Transform>();
But now i have the Problem that he also us the $$anonymous$$ainComponent and add it to the Array of Waypoints. Someone idea how i only can load the Transform of the Childrens?
Your answer
Follow this Question
Related Questions
Why is my List adding to same array object again and again in update? 1 Answer
set Random 0 Answers
Array.Sort() - JS only???! 0 Answers
Next and Back Button? 0 Answers
Visual Studio issue - Universal windows apps build. Branding 0 Answers