Nearest object code not falling back on alternate in OnGUI()

Hi guys,

I have a compass that points to the nearest checkpoint in my game. If all the checkpoints have been passed (and therefore removed), it’s supposed to point to the finish loop. This is the relevant code:

`
function GetClosestObject(tag:String, altTag:String) : GameObject
{
    var objectsWithTag = GameObject.FindGameObjectsWithTag(tag);
    
    if (!objectsWithTag)
    	GameObject.FindGameObjectsWithTag(altTag);
    
    var closestObject : GameObject;
    
    for (var obj : GameObject in objectsWithTag)
    {
        if(!closestObject)
        {
           closestObject = obj;
        }
        //compares distances
        if(Vector3.Distance(transform.position, obj.transform.position) <= Vector3.Distance(transform.position, closestObject.transform.position))
        {
           closestObject = obj;
        }
    }
    return closestObject;
}

function OnGUI() 
{

	target = GetClosestObject("Checkpoint", "Finish").transform;
        ...
}
`

It works fine as long as there is at least one checkpoint. However, when there is only the finish loop remaining (which has the tag “Finish”), the arrow dial disappears and prints the following console error:

`
NullReferenceException: Object reference not set to an instance of an object
`

What seems to be the problem? All the game objects have their tags set right in the scene.

MachCUBED
DirectionArrow.OnGUI () (at Assets/Entities/Player/Character/Scripts/DirectionArrow.js:44)

Shouldn’t that be…?

if (!objectsWithTag)
	objectsWithTag = GameObject.FindGameObjectsWithTag(altTag);

Try this:

function GetClosestObject(tag:String, altTag:String) : GameObject
{
    var objectsWithTag = null;
    
    objectsWithTag = GameObject.FindGameObjectsWithTag(tag);

    if (objectsWithTag == null) 
    {
    	Debug.Log("Finding finish");
		objectsWithTag = GameObject.FindGameObjectsWithTag(altTag);
	}
    
    var closestObject : GameObject;
    
    for (var obj : GameObject in objectsWithTag)
    {
        if(!closestObject)
        {
           closestObject = obj;
        }
        //compares distances
        if(Vector3.Distance(transform.position, obj.transform.position) <= Vector3.Distance(transform.position, closestObject.transform.position))
        {
           closestObject = obj;
        }
    }
    return closestObject;
}

It probably doesn’t work, since FindGameObjectsWithTag would return ‘null’ anyway, but it’s worth a try…

Try doing this:

function GetClosestObject(tag:String, altTag:String) : GameObject
{
    var objectsWithTag;
    
    Debug.Log("Finish loop:" + GameObject.FindGameObjectsWithTag(altTag));

    objectsWithTag = GameObject.FindGameObjectsWithTag(tag);
    	
    if (objectsWithTag.Length == 0)
    {
       Debug.Log("Finding finish");
       objectsWithTag = GameObject.FindGameObjectsWithTag(altTag);
    }
    
    var closestObject : GameObject;
    
    for (var obj : GameObject in objectsWithTag)
    {
        if(!closestObject)
        {
           closestObject = obj;
        }
        //compares distances
        if(Vector3.Distance(transform.position, obj.transform.position) <= Vector3.Distance(transform.position, closestObject.transform.position))
        {
           closestObject = obj;
        }
    }
    return closestObject;
}