Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 cipherr · Aug 04, 2012 at 06:38 AM · nullreferenceexceptionfindgameobjectswithtag

Finding multiple objects with a tag and NullReferenceException error

Hey guys. I'm sort of a new unity user, and have just been playing around with scripting and whatnot to get a hang of things before I pursue any actual projects. I've been stuck on this error for a few hours now and have sifted through these answer forums, as well as the scripting manual, but can't seem to solve this issue.

Okay, so here is what my script does. If you left click, you shoot boxes at a rapid pace from your position (these boxes have gravity and physics and all that). If you right click, it creates a single, static box that cannot move, that you can use as a platform. What I'm trying to do is make it so when you press the middle mouse button, all of these static blocks regain their gravity/physics and fall to the ground. Unfortunately, I keep getting a NullReferenceException: Object reference not set to an instance of an object error. Anyways, here is my code:

 var spawnBox : boolean = false;
 
 var spawningCube: Transform;
 var spawningCube2: Transform;
 var cubeSpawnHere : Transform;
 var cubeSpawnHere2 : Transform;
 
 
 function Update() {
  
  var staticCubes = GameObject.FindGameObjectsWithTag ("freezeCube");
 
  if (Input.GetKey("mouse 0")) {
  spawnBox = true;
  Instantiate(spawningCube, cubeSpawnHere.position, cubeSpawnHere.rotation);
  }
  else {
  spawnBox = false;
  }
  
  if (Input.GetKeyDown("mouse 1")) {
  spawnBox = true;
  Instantiate(spawningCube2, cubeSpawnHere2.position, cubeSpawnHere2.rotation);
  }
  else {
  spawnBox = false;
  }
  if (Input.GetKey("mouse 2")) {
  staticCubes.rigidbody.isKinematic = false;
  Debug.Log("test");
  } else { 
  staticCubes.rigidbody.isKinematic = true;
  }
 }


The error links back to the "staticCubes.rigidbody.isKinematic = true;" line. And so it's clear, this script is on the built in first person controller. The cube in question has no script, but has the tag "freezeCube". Is there one little error, or am I approaching this wrong to begin with? Should I be making all the freeze cubes that spawn part of an array? Any tips would be most helpful. Thanks.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Gargorn · Aug 05, 2012 at 09:59 AM

So I have modified your code to work so the errors don't occur. The problem was that it was trying to modify an array of game objects. To make this work it would have to modify them one by one. You can see in the code that I have fixed this.

 var spawnBox : boolean = false;
 var spawningCube: Transform;
 var spawningCube2: Transform;
 var cubeSpawnHere : Transform;
 var cubeSpawnHere2 : Transform;
  var staticCubes : GameObject[];
 
 function Update() 
 {
 
 staticCubes = GameObject.FindGameObjectsWithTag ("freezeCube");
 
  if (Input.GetKey("mouse 0")) 
   {
   spawnBox = true;
   Instantiate(spawningCube, cubeSpawnHere.position, cubeSpawnHere.rotation);
   }
  else 
   {
   spawnBox = false;
   }
 
  if (Input.GetKeyDown("mouse 1")) 
   {
   spawnBox = true;
  Instantiate(spawningCube2, cubeSpawnHere2.position, cubeSpawnHere2.rotation);
   }
  else 
   {
   spawnBox = false;
   }
  if (Input.GetKey("mouse 2")) 
   {
   for (var staticCube : GameObject in staticCubes)  
   { 
   staticCube.rigidbody.isKinematic = false;
   Debug.Log("test");
   }
  } 
 }

Comment
Add comment · Show 1 · 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 cipherr · Aug 05, 2012 at 09:15 PM 0
Share

Yup, that did it! I'm still learning, and I haven't really gotten into the area of arrays too much yet, but I see how this works. Thanks a lot for the help.

avatar image
0

Answer by Gargorn · Aug 04, 2012 at 09:00 AM

What I am wondering is if the static cubes have a Rigid Body component attached to them.

The error is basically saying that it cannot find what you are referencing so I am guessing it either can't find any cubes tagged with freezeCube or the static cubes don't have Rigid Body components.

Comment
Add comment · Show 1 · 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 cipherr · Aug 04, 2012 at 09:08 PM 0
Share

Yeah the only different between the two cubes is that the staticCube has the "Is $$anonymous$$inematic" box unchecked in the Rigidbody component

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

looping through game objects 1 Answer

GetComponent NullReferenceException 2 Answers

Works in player but NullReferenceException in build. 2 Answers

WWW.CheckSecurityOnHeaders throwing NullReferenceException 1 Answer

NullReferenceException? 2 Answers


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