x


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.

more ▼

asked Aug 04 '12 at 06:38 AM

cipherr gravatar image

cipherr
5 1 2 5

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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");
  }
 } 
}
more ▼

answered Aug 05 '12 at 09:59 AM

Gargorn gravatar image

Gargorn
21

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.

Aug 05 '12 at 09:15 PM cipherr
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Aug 04 '12 at 09:00 AM

Gargorn gravatar image

Gargorn
21

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

Aug 04 '12 at 09:08 PM cipherr
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x394
x53

asked: Aug 04 '12 at 06:38 AM

Seen: 265 times

Last Updated: Aug 05 '12 at 09:15 PM