- Home /
GameObject Instantiation - NullReferenceException
I am trying to instantiate an object in front of the camera whenever the player changes the item they are holding from the inventory. I keep getting the following error:
NullReferenceException: Object reference not set to an instance of an object
(Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name) UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name) InventoryScript.LateUpdate () (at Assets/Scripts/InventoryScript.js:153))
I have modified the script so that it just requires an object to be selected in the editor and keep getting the error. The plan is to use the object ID to point to the correct GameObject in an array of GameObjects.
This is my script, the error occurs on Line 153, the GameObject is set to the prefab of a pan at the moment.
Any ideas? Thanks in advance
import System.Collections.Generic;
var maincamera : GameObject;
//handles adding of items
var AddNewItem : boolean = false;
var AddNewItem_id : int = 0;
//handles item selection from main inventory
var equip_selection : int = 0;
var print_size : int;
var timer : float = 0.1;
var found_slot_equip : boolean = false;
var found_slot_inv : boolean = false;
var slot_to_equip : int;
var slot_to_inv : int;
var inventory_visible : boolean = false;
var equip_visible : boolean = false;
var input_timer : float = 0.1;
var update_timer : float = 0.1;
var equip_view_timer : float = 1;
var Obj_inst : int;
var Obj_inst_prefab : GameObject;
var ActiveObject : GameObject;
var copy_from_actual : int;
//handles the scrollable print array
var stagger : int;
var limit : int;
var mid_point : int;
//holding all of our items
var items : Item[];
var world_pf = new GameObject[16];
var hold_pf = new GameObject[16];
var inventory_size : int = 30;
var inventory_items : int = 0;
var equip_size : int = 9;
var equip_items : int = 0;
//changeing of gameobjects
var change_holding_item : boolean = false;
//inventory
var MainInventory : List.<Item> = new List.<Item>();
var EquipMenu : List.<Item> = new List.<Item>();
function Start ()
{
mid_point = ((equip_size - 1) / 2);
stagger = equip_selection - mid_point;
limit = equip_size - 1;//limit to copy from
//fill the arrays
for(var n = 0; n < inventory_size; n++)
{
MainInventory.Add(items[0]);
inventory_items = 0;
}
for(var b = 0; b < equip_size; b++)
{
EquipMenu.Add(items[0]);
}
update_print = true;
}
function LateUpdate ()
{
if( input_timer < 0)
{
//---------------TOGGLE INVENTORY SHOW/HIDE------------------
if(Input.GetButton("Inventory"))
{
if( inventory_visible)
{
inventory_visible = false;
}
else
{
inventory_visible = true;
}
}
input_timer = 0.1;
}
if( update_timer < 0)
{
//---------------ADD NEW ITEMS------------------
if( AddNewItem)
{
//find empty slot to place item
for(var f = -inventory_size; f > 0; f++)
{
found_slot_inv = false;
if(MainInventory[f].Name == "Empty Slot" && !found_slot_inv)
{
slot_to_inv = f;
found_slot_inv = true;
}
}
//place item in empty slot
MainInventory[slot_to_inv] = items[AddNewItem_id];
AddNewItem = false;
AddNewItem_id = 0;//default to empty slot incase
}
//---------------CHANGE SELECTION WITH MOUSEWHEEL------------------
if( Input.GetAxis("Mouse ScrollWheel") > 0)
{
equip_selection++;
equip_view_timer = 1;
change_holding_item = true;
}
if( Input.GetAxis("Mouse ScrollWheel") < 0)
{
equip_selection--;
equip_view_timer = 1;
change_holding_item = true;
}
//---------------HANDLE INVENTORY LIMITS FOR SELECTION------------------
if( equip_selection > equip_size - 1)
{
equip_selection = 0;
}
if( equip_selection < 0)
{
equip_selection = equip_size - 1;
}
//---------------EQUIP INVENTORY ONLY VISIBLE FOR A FEW SECONDS------------------
if( equip_view_timer < 0 && equip_visible)
{
equip_visible = false;
}
else if( equip_view_timer > 0 && !equip_visible)
{
equip_visible = true;
}
//---------------OBJECT INSTANTIATION WHEN SELECTION CHANGES------------------
if (change_holding_item)
{
Instantiate( Obj_inst_prefab ,
maincamera.transform.position,
Quaternion.Euler( maincamera.rotation.eulerAngles.x,maincamera.rotation.eulerAngles.y,maincamera.rotation.eulerAngles.z));
change_holding_item = false;
}
update_timer = 0.1;
}
update_timer -= Time.deltaTime;
input_timer -= Time.deltaTime;
equip_view_timer -= Time.deltaTime;
}
function OnGUI ()
{
//---------------PRINT THE MAIN INVENTORY + HANDLE OBJECT MANAGEMENT------------------
if( inventory_visible)
{
print_size = inventory_size;
var y = 0;//variables for grid structure
var n = 0;
for(var x = 0; x < print_size; x++)
{
if(y > 4){y=0;n++;}
if( GUI.Button(Rect(Screen.width/2 - 125 + (50 * y), Screen.height/2 - 150 + (50 * n), 50, 50), MainInventory[x].Name ))
{
found_slot_equip = false;
//equip to empty slot
if( Input.GetButton("lshift"))
{
for( var g = 0; g < (equip_size - 1); g++)
{
if( EquipMenu[g].Name == "Empty Slot" && !found_slot_equip)
{
slot_to_equip = g;
found_slot_equip = true;
}
}
}
//equip to selected slot
if( Input.GetButton("lshift") == false)
{
found_slot_equip = true;
}
//if slot is found then equip the item
if( found_slot_equip)
{
var item : Item = EquipMenu[slot_to_equip];
//set the new equip item
EquipMenu[slot_to_equip] = MainInventory[x];
//set the new inventory item
MainInventory[x] = item;
//for empty slots transferred update variables
if( item.Name == "Empty Slot")
{
inventory_items--;
equip_items++;
}
}
}
y++;
}
}
//---------------PRINT THE EQUIP MENU DEPENDING ON CURRENT SELECTION-------------------
if( equip_visible)
{
for(var v = 0; v < equip_size; v++)
{
var index = TranslateSelection(v);
if( GUI.Button(Rect(Screen.width - 100, Screen.height/2 - 150 + (50 * v), 100, 50), EquipMenu[index].Name ))
{
}
}
}
//---------------PRINT CURRENT ITEM WE ARE USING------------------
GUI.Label(Rect(Screen.width - 100, Screen.height - 50, 100, 20), "selection" );
}
function TranslateSelection ( iteration : int )
{
//---------------TRANSLATE ITERATION TO THE ITEM INDEX FOR PRINTING-----------------
var translation = equip_selection - mid_point;//iteration + translation = item to print
var index_toprint = iteration + translation;
var upperlimit = equip_size - 1;
var lowerlimit = 0;
if( index_toprint > upperlimit)
{
index_toprint -= equip_size;
}
if( index_toprint < lowerlimit)
{
index_toprint += equip_size;
}
return index_toprint;
}
Assu$$anonymous$$g you've verified that 'Obj_inst_prefab' and maincamera' are getting set in the Inspector, the most likely problem is that the script is also attached to another game object (one you are not aware of). Click in the search box in the upper right corner of the Hierarchy window and search for this script/component.
P.S. Ins$$anonymous$$d of using Quaternion.Euler() on line 155, you can just pass maincamera.rotation for the rotation parameter.
I can verify that the "InventoryScript" is only attached to the PlayerCapsule. I also have the GameObjects set:
Answer by ArkaneX · Jan 08, 2014 at 11:45 PM
The problem is that maincamera
is GameObject
, and GameObject
has no rotation
property. It's strange that Unity compiled your code...
excellent and thankyou! i now have hundreds of pans spawning as i scroll :D
Could you share how you managed to compile this script? I remember answering similar question some time ago, but still have no idea how it was possible.
i have no idea? the entire script has been uploaded above..
In my case your script doesn't compile and it's not possible to play. I guess for now this will remain mystery, but thanks anyway :)
Your answer
Follow this Question
Related Questions
Checking if object intersects? 1 Answer
Make prefab appear at start/awake 2 Answers
Instantiate Projector at Normal Direction 0 Answers
Endless 3D plane repetition animated by script is not moving 0 Answers