looping through game objects

I am trying to loop through a bunch of tagged objects. For each object, I am creating a string by grabbing attributes from a script attached to each object. I’ve tried looping through the children of a transform and now the tagged objects. In both cases, I get a NullReferenceException.

function formatSketchLinkString ()
    {
    	var outputString : String;
    	var sketchLinkObjects : GameObject[];
    	sketchLinkObjects = GameObject.FindGameObjectsWithTag("sketchLink");
    	var sketchLinkScript;
    	for (i=0;i<sketchLinkObjects.length;i++)
    	{
    		sketchLinkScript = sketchLinkObjects*.GetComponent("sketchLink");*
  •  if (i == sketchLinkObjects.length-1)*
    
  •  {*
    
  •  	outputString+=sketchLinkScript.startJointName+","+sketchLinkScript.endJointName;*
    
  •  }*
    
  •  else*
    
  •  {*
    
  •  	outputString+=sketchLinkScript.startJointName+","+sketchLinkScript.endJointName+":";*
    
  •  }*
    
  • }*

  • return outputString;*
    }
    If I loop through and simply ask for the name of each of the objects, I get the expected number of objects and the expected names. And, I know that each of these objects has the script “sketchLink” associated with it …
    function formatSketchLinkString ()
    {

  • var outputString : String;*

  • var sketchLinkObjects : GameObject;*

  • sketchLinkObjects = GameObject.FindGameObjectsWithTag(“sketchLink”);*

  • var sketchLinkScript;*

  • for (i=0;i<sketchLinkObjects.length;i++)*

  • {*
    _ print(sketchLinkObjects*.name);_
    _
    }*_

* return outputString;*
}
Any ideas? I am dying on this one!!
I added this last night and it works. This is a serious “hack” … why would this work and the previous code does not?!!
function formatSketchLinkString ()
{
* var outputStrings : String[];*
* var sketchLinkObjects : GameObject[];*
* sketchLinkObjects = GameObject.FindGameObjectsWithTag(“sketchLink”);*
* outputStrings = new String[sketchLinkObjects.length];*
* var sketchLinkScript;*
* for (i=0;i<sketchLinkObjects.length;i++)*
* {*
_ sketchLinkScript = sketchLinkObjects*.GetComponent(“sketchLink”);*
outputStrings = sketchLinkScript.connectivityString;_

* }*
* var outputString : String;*
* for (i=0;i<outputStrings.length;i++)*
* {*
* if (i==outputStrings.length-1)*
* {*
_ outputString+=outputStrings*;
}
else*

* {
outputString+=outputStrings+“:”;
}
}*_

* return outputString;*
}

You “know” that every one have such a script attached? :smiley:

How about checking if the Getcomponent result is null?

I’m not using JS that often but I think it’s better to put a var infront of i.

Btw: If your tagged-objects are the only one with that script attached you can use FindObjectsOfType which gives you an array that holds the direct script references:

var objects : sketchLink[] = FindObjectsOfType(sketchLink) as sketchLink[];
for (var i = 0; i < objects.length; i++)
{
    print(objects*.startJointName);*

}