Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by wbl1 · Jun 17, 2011 at 03:39 AM · nullreferenceexceptionchildrenfor-loopfindgameobjectswithtag

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;
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Jun 17, 2011 at 05:04 AM 0
Share

"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. ;) )

avatar image Bunny83 · Jun 17, 2011 at 05:17 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

List becomes null in Parent when filled in child script,List becomes null within own class after being filled in other class 0 Answers

Is it possible to FindWithTag only within children of a certain gameObject? 1 Answer

Finding multiple objects with a tag and NullReferenceException error 2 Answers

Split() into a fixed length array? 1 Answer

For Loop and Null referance error Ios Scripting 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges