• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by winch.91 · Jan 08, 2014 at 09:54 PM · instantiatenullreferenceexeption

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.DynamicDispatc$$anonymous$$ng.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatc$$anonymous$$ng.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.

T$$anonymous$$s 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("ls$$anonymous$$ft"))
             {    
                 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("ls$$anonymous$$ft") == 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;
 
 
 
 }
Comment

People who like this

0 Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Jan 08, 2014 at 10:37 PM 0
Share

Assuming 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. Instead of using Quaternion.Euler() on line 155, you can just pass maincamera.rotation for the rotation parameter.

avatar image winch.91 · Jan 08, 2014 at 10:51 PM 0
Share

I can verify that the "InventoryScript" is only attached to the PlayerCapsule. I also have the GameObjects set:alt text

scrn1.png (27.5 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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...

Comment

People who like this

0 Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image winch.91 · Jan 08, 2014 at 11:51 PM 0
Share

excellent and thankyou! i now have hundreds of pans spawning as i scroll :D

avatar image ArkaneX · Jan 08, 2014 at 11:55 PM 1
Share

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.

avatar image winch.91 · Jan 09, 2014 at 07:32 PM 0
Share

i have no idea? the entire script has been uploaded above..

avatar image ArkaneX · Jan 10, 2014 at 09:19 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Checking if object intersects? 1 Answer

Invisable Projectile; Instantiate at Collisions 2 Answers

Help with a different approach to instantiating weapons? 1 Answer

Instantiating in a circle? 0 Answers

Parenting an instantiated Prefab? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges