Myhijim
September 27, 2012, 11:27am
1
Hey all i’ve written a basic script and it keeps giving me this error.
NullReferenceException: Object reference not set to an instance of an object
scrGUI.CreateGrid () (at Assets/[Scripts]/Player - Scripts/scrGUI.js:41)
scrGUI.Main () (at Assets/[Scripts]/Player - Scripts/scrGUI.js:26)
I will outline the important parts
Where the error is:
var grid : InvGrid[];
grid = new InvGrid[totalSlots];
CreateGrid();
function CreateGrid()
{
var slotW = 50;
var slotH = 50;
var startX = (-4 * slotW);
var startY = screenH + 230;
for(var i=0;i<1;i++)
{
print("Commencing");
grid[0].minX = screenW + startX + (i*slotW);
grid[0].maxX = screenW + startX + (i*slotW) + slotW;
grid[0].minY = startY;
grid[0].maxY = startY - 50;
print("Done");
}
}
The class is outlined at the bottom of the script
class InvGrid
{
var minX = 0;
var maxX = 0;
var minY = 0;
var maxY = 0;
}
I see no reason for it not to work… I have used the same layout in other scripts. Is it just unity?
Any help is really appreciated, Thanks
You’ve told grid that it will be holding InvGrid classes, but you’re trying to alter something that isn’t there yet.
print("Commencing");
grid[0] = new InvGrid (); // Added this line
grid[0].minX = screenW + startX + (i*slotW);
grid[0].maxX = screenW + startX + (i*slotW) + slotW;
grid[0].minY = startY;
grid[0].maxY = startY - 50;
print("Done");
By the way, I think you mean ‘i’ not ‘0’ in the array index.
Myhijim
September 27, 2012, 11:41am
3
I do mean i, 0 was just for testing purposes.
grid was set above that code, does that affect it?
P.S same error anyway
Is that your exact code? You shouldn’t be calling functions out there!
Place your call to CreateGrid inside of Start or Awake.
Myhijim
September 27, 2012, 11:53am
5
My complete code US
//Mouse Vector2 Screen
var mouseVec : Vector2;
var screenH : int;
var screenW : int;
screenH = Screen.height/2;
screenW = Screen.width/2;
//Parent Variables
var invScript : scriptPlayer;
invScript = transform.parent.GetComponent(scriptPlayer);
//Inventory Variable
var invOpen = false;
var totalSlots : int; totalSlots = invScript.invSlots;
var hotbarLen : int;
CreateGrid();
function CreateGrid()
{
var slotW = 50;
var slotH = 50;
var startX = (-4 * slotW);
var startY = screenH + 230;
var grid : InvGrid[];
grid = new InvGrid[totalSlots];
for(var i=0;i<1;i++)
{
print("Commencing");
grid[i].minX = screenW + startX + (i*slotW);
grid[i].maxX = screenW + startX + (i*slotW) + slotW;
grid[i].minY = startY;
grid[i].maxY = startY - 50;
print("Done");
}
}
////////////////////////////////////////////
//Update
////////////////////////////////////////////
function Update()
{
mouseVec = Vector2(Input.mousePosition.x,Input.mousePosition.y);
screenH = Screen.height/2;
screenW = Screen.width/2;
}
////////////////////////////////////////////
//Inv Grid Class
////////////////////////////////////////////
class InvGrid
{
var minX = 0;
var maxX = 0;
var minY = 0;
var maxY = 0;
}
Should work right?
P.S Going to try on a different computer, to eliminate that posibility
Myhijim:
My complete code US
Should work right?
No, do as I suggested.
// instead of the call on line 20
function Start () {
CreateGrid ();
}
Let me guess, you took out the
grid[0] = new InvGrid (); // Added this line
It is still needed.
EDIT: Looking at your code, why did you move
var grid : InvGrid[];
grid = new InvGrid[totalSlots];
inside the function? You won’t be able to access it from anywhere else now.
Myhijim
September 27, 2012, 12:47pm
9
1.Nope. Did not take that out.
2.Changed that after I posted it
That’s odd, I took the code in your OP, placed CreateGrid inside Start and created an InvGrid instance at index 0 and it worked fine for me.
Myhijim
September 27, 2012, 12:51pm
11
Wow.
That was extremely stupid of me i should have known better… There was nothing defining grid like you said.
THANK YOU THANK YOU THANK YOU
You’ve been a great help
Result Code:
var grid : InvGrid[];
grid = new InvGrid[totalSlots];
CreateGrid();
////////////////////////////////////////////
//Create Grid
////////////////////////////////////////////
function CreateGrid()
{
var slotW = 50;
var slotH = 50;
var startX = (-4 * slotW);
var startY = screenH + 230;
for(var i=0;i<8;i++)
{
print("Commencing");
grid[i] = new InvGrid();
grid[i].minX = screenW + startX + (i*slotW);
grid[i].maxX = screenW + startX + (i*slotW) + slotW;
grid[i].minY = startY;
grid[i].maxY = startY - 50;
print("Done");
}
}