- Home /
If one point is taken go the other point. Quick fix?
Hello everybody,
I know that this is one of the questions I should be able to resolve after almost one year of coding, but this project is endless and has so much in it, so looking for help here:
How can I write the logic for if one way point from the array of transform points is taken the AI goes to the next in line?
Thanks
Answer by Jwizard93 · Jul 10, 2017 at 08:28 PM
Many ways. You could store a seperate array of bools of the same length. A bool at any given index could represent whether the transform point at that same index in the other array is taken or not.
Or you instead of storing an array of transform points you could store a struct that has a transform point and a bool.
Either of those sound good?
Hi, Yes I'm going to use a struct with transform and a bool
Hi again,
I've added a script such as this. Now I need to reference this wayPoints from
public TargetPoint[] waypointTransforms;
private void InitWayPoints()
{
if (transform.childCount>0)
{
waypointTransforms = new TargetPoint[transform.childCount];
for (int i = 0; i < waypointTransforms.Length; i++)
{
waypointTransforms[i] = new TargetPoint(transform.GetChild(i));
}
}
else
{
Debug.LogWarning("No way points found in the scene");
Debug.Break();
}
}
public struct TargetPoint //I've made a struct with transform and bool to be referenced farther
{
public Transform targetTransform;
public bool s_taken;
public TargetPoint(Transform s_transform)
{
targetTransform = s_transform;
s_taken = false;
}
}
This line is calling the next way point but I get an error of null reference... obviously I'm trying to call a transform but it's calling an array of structs.
nextWayPoint = Random.Range(0, SceneIndex.instance.waypointTransforms.Length);
Answer by webcam · Jul 10, 2017 at 08:26 PM
Taken by another AI I'm assuming? Just make a small info class for each point with a reference to a transform and an taken bool. If you want to get really fancy make is a serializable class and it will display nicely in a list and you can still reference your transforms in the inspector.
Then it's as simple as looping through the list until you find a taken == false and go there and set taken to true. Reset it when you leave.