[solved] c# script : get a return val (waypoint example)

Hello all,

I need to increase my c# knowledge, I can’t really understand how static and return values are meant to be declared…

The following script uses a simple waypoints system, really cool as it doesn’t require to assign waypoints manually, nore to set the array count, but just picks up all the transforms from a given game object.
:°D

Now I’m attempting to get the closest waypoint from the array list.
Array is working properly. tmpDist and Dist are correct too…
But the “FindClosest” function is not working yet.
;°(

I guess I need a little explanation about how to deal with array items
:°p

  • Should the FindClosest function be static or void?
  • what is to be declared within brackets in the function FindClosest ()?

Thanks in advance for your help…


using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class Waypoints : MonoBehaviour {

	public Transform myTransform;
	
	public GameObject waypointContainer;
	List <Transform> waypoints ;
	Transform[] potentialWaypoints;
	public float Dist;
	public float tmpDist;
	public int bestWaypoint;
	public int currentWaypoint;
	public float currentAngle;
	Vector3 RelativeWaypointPosition;
	Vector3 RelativeWaypointDir;


//////////////////////////// waypoints ///////////////////////


	void Start ()
	{
		GetWaypoints();
		FindClosest(); ///// tried this (bestWaypoint); //(potentialWaypoints[]);
		currentWaypoint = bestWaypoint;
	}
	
	void GetWaypoints ()
	{
		if (waypointContainer!=null) 
		{
			Transform[] potentialWaypoints = waypointContainer.GetComponentsInChildren <Transform> ();

			waypoints = new List <Transform> ();

			foreach (Transform potentialWaypoint in potentialWaypoints) 
			{
				for (int i=0;i<waypoints.Count;i++);
				if (potentialWaypoint != waypointContainer.transform)
					waypoints.Add (potentialWaypoint);
			//	Debug.Log("foundpotentialWaypoint");
			}
		}
	}	
		
	void FindClosest () {  /////tried this: (Transform[] waypoints) {
		
		Dist = (waypoints[0].transform.position - transform.position).sqrMagnitude;
		waypoints[bestWaypoint] = waypoints[0];
			
		for (int i=1;i<waypoints.Count;i++) {
			tmpDist = (waypoints*.transform.position - transform.position).sqrMagnitude;*
  •  	if (tmpDist < Dist)*
    

_ waypoints[bestWaypoint] = waypoints*;_
_
}_
_
// return waypoints[bestWaypoint];_
_
}*_

You cannot have the FindClosest function be static because it operates on non-static members.

It seems odd to me that you are shuffling around the waypoints in the FindClosest function. You are always inadvertently overwriting the first waypoint in the list with one of the other ones whenever a waypoint is found to be closer. Instead you might want to use bestWaypoint to be the index of the best waypoint and not just be basically a constant zero moving the best waypoint into that index. Example:

   void FindClosest () {

       Dist = (waypoints[0].transform.position - transform.position).sqrMagnitude;
       bestWaypoint = 0;

       for (int i=1;i<waypoints.Count;i++) {
         tmpDist = (waypoints*.transform.position - transform.position).sqrMagnitude;*

if (tmpDist < Dist)
bestWaypoint = i;
}
}

Thanks for your reply.

I did try this option, but I get a strange result : In my array configuration, myTransform stands nearby WP n°5 but FindClosest function returns WP n° 401 as the closest. As WP n° 401 is the last one in the loop, it is actually the closest to WP n°0... so it looks like the FindClosest returns the closest to WP n°0 instead of the closest to myTransform... If I move WP n°0 next to WP n°400, then the closest WP found is the n°400.

I updated this btw, but it has no further effect:

tmpDist = (Waypoints*.transform.position - myTransform.position).sqrMagnitude;

* *

So I guess I'm not far from the solution, but I still wonder why... uh... cruel world...

* *

Thanks again for your kind attnetion and compassion for newbies ;°)

* *

///////////// edit

* *

Well, I found the fix:* *I forgot to assign the Dist = tmpDist as it can update the lowest item found in the array... Obvious. :°D

* *

Here is the updated FindClosest function. I hope it can be usefull to other, as it's quite rare to find c# waypoints system, mainly js...

* *```* *void FindClosest () {* *Dist = Mathf.Infinity;* *// Dist = (Waypoints[0].transform.position - myTransform.position).sqrMagnitude;* *for (int i=0;i