- Home /
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[i].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[i].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[i].GetComponent("sketchLink");
outputStrings[i] = sketchLinkScript.connectivityString;
}
var outputString : String;
for (i=0;i<outputStrings.length;i++)
{
if (i==outputStrings.length-1)
{
outputString+=outputStrings[i];
}
else
{
outputString+=outputStrings[i]+":";
}
}
return outputString;
}
Answer by Bunny83 · Jun 17, 2011 at 04:51 AM
You "know" that every one have such a script attached? :D
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[i].startJointName);
}
"var" is implicit the first time you assign a value to a variable, and thus unnecessary. (Unless you use "#pragma strict", in which case it won't allow that. Unless you add "#pragma implicit", in which case it will. ;) )
Yeah, i knew you can omit var
(another reason why i don't use JS... :D i have to write them down the other day, can't remember them all ;))
Anyway, i can't see another reason for a Null-Ref-Exception besides GetComponent.
$$anonymous$$aybe the script name is not "right"? case sensitive? SketchLink?
Your answer
![](https://koobas.hobune.stream/wayback/20220612110733im_/https://answers.unity.com/themes/thub/images/avi.jpg)