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.

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.

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