- Home /
What is wrong with my script?
I'm getting a: NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible) Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value) Boo.Lang.Runtime.RuntimeServices.UnboxSingle (System.Object value) MapGrid.Drill () (at Assets/Scripts/MapGrid.js:44) MapGrid.Update () (at Assets/Scripts/MapGrid.js:39). And my script is not working. Can someone give me a hint what is wrong?
var prefab : GameObject;
var player : GameObject;
var gridX = 5;
var gridY = 5;
private var spacing = 1;
var UseSpawner : boolean = true;
var Spawn : GameObject;
var gridXRounded;
var SpawnX;
var SpawnY;
var block : GameObject;
function Start ()
{
grid2D = new GameObject[gridX,gridY];
for (var y = 0; y < gridY; y++){
for (var x = 0; x < gridX; x++){
var pos : Vector3 = Vector3(x,y,0) * spacing;
var newBlock : GameObject = Instantiate(prefab, pos, Quaternion.identity);
newBlock.name = "GridBlock" + x + "-" + y;
}
}
if(UseSpawner){
var SpawnX : int = Mathf.Round(gridX / 2 - 1);
var SpawnY : float = gridY + 0.5;
player.transform.localScale = Vector3(0.75, 0.75, 0.75);
Spawn.transform.position = Vector3(SpawnX, SpawnY, 0 );
player.transform.position = Vector3(SpawnX, SpawnY, 0);
}
}
function Update() {
if(Input.GetButton("Down")){
Drill();
}
}
function Drill(){
block = GameObject.Find("GridBlock" + Mathf.Round(player.transform.x) + "-" + Mathf.Round(player.transform.y - 1));
Destroy(block);
}
Answer by Khada · Mar 10, 2013 at 10:19 AM
It would seem that 'block' is null inside your 'Drill' method. Put a break point on line line 44 and check it's value. It may be that your character is attempting to drill at a location where there is no block to destroy, thus you should add a sanity check inside your 'Drill' method:
function Drill()
{
block = GameObject.Find("GridBlock" + Mathf.Round(player.transform.x) + "-" + Mathf.Round(player.transform.y - 1));
if(block != null)
Destroy(block);
}
EDIT 1:
It also looks like your 'player' object may be null as your script doesn't assign it anywhere in the code you provided. You should assign it in the 'Start' method.
EDIT 2:
Further inspection makes me think that you need to cast your float values to ints as you seem to have used ints when naming the blocks. Line 44 should look like:
block = GameObject.Find("GridBlock" + parseInt(Mathf.Round(player.transform.x)) + "-" + parseInt(Mathf.Round(player.transform.y - 1)));
I have added it to my script but it's still not working. It is also giving me this error again: NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible) Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value) Boo.Lang.Runtime.RuntimeServices.UnboxSingle (System.Object value) $$anonymous$$apGrid.Drill () (at Assets/Scripts/$$anonymous$$apGrid.js:44) $$anonymous$$apGrid.Update () (at Assets/Scripts/$$anonymous$$apGrid.js:38)
Actually, it looks like it may be the player object that is null as I don't see you assigning it anywhere (or it could be both). Do you set player somewhere else? If not, you need to assign it before you use it.
I move my player around, so i guess it could be. But how do you assign it?
Well you can either assign it in the inspector (which you may already be doing) or you can use GameObject.Find to get it by name/tag etc.
Seriously though, use a breakpoint to check all the variables on line 44 and see which one is null.
Here is info on debugging if you need. Basically just attack the unity process to your mono program, press f9 with the cursor on line 44 of that script and run the game. Hover the mouse cursor in mono to see the value of an object.
I did that and it says the the cube = {null}, but I have no idea how to fix that...
Your answer
Follow this Question
Related Questions
enemy mechanics script and declining health 1 Answer
Windows Script error upon opening a new javascript behavior script. 4 Answers
Problems with java script. 1 Answer
Error in script 1 Answer
syntax errors 1 Answer