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;*
}