Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by topkekker · Oct 22, 2014 at 02:22 PM · c#inventory

Object Reference not set to an instance of object

Hey guys, So im getting the error in the title.

I'm trying to create a numeric inventory system(for keys 1,2,3,4 and 5).

Here is my code.

 using UnityEngine;
     using System.Collections;
 
     public class playerInventory : MonoBehaviour {
         public int currentWeapon;
         public GameObject[] weapons;
         public int selected = 0;
 
         // Use this for initialization
         void Start () {
         
         }
         
         // Update is called once per frame
         void Update () {
             loopThroughBackpack ();
         }
 
         void loopThroughBackpack(){
             if (Input.GetKeyDown (KeyCode.Alpha1)) {
                 selected = 0;
                 Debug.Log (weapons[selected]);
             }
             else if(Input.GetKeyDown(KeyCode.Alpha2)){
                 selected = 1;
                 Debug.Log(weapons[selected]);
                 Instantiate (weapons[selected], weapons[selected].transform.parent.position, transform.rotation );
 
             }
 
             
 
                 }        
             }
         
 

What im trying to do is spawn a asset called canteen and then parent it to the First person controller's Main Camera.

Any help is really appreciated!

Comment
Add comment · Show 5
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 richyrich · Oct 22, 2014 at 02:25 PM 0
Share

Have you tried looping through the array to check for null references? - there could be a problem with the way you set it up

avatar image Marcel81 · Oct 22, 2014 at 04:15 PM 0
Share

Have you assigned GameObjects to the weapons array? Especially to weapons[1]?

avatar image b1gry4n · Oct 22, 2014 at 05:53 PM 0
Share

$$anonymous$$arcel is probably right. In arrays your first item is 0, second is 1, so on. You are trying to switch from 0 to 1. If youre getting a null reference you need to assign another item to your array to take the "1" slot. To test this you could just place the same item in the "1" slot (so you have 2 canteens) and your debug.log messages would ensure your code is working

avatar image wijesijp · Oct 22, 2014 at 06:09 PM 0
Share

Does public GameObject[] weapons; hold prefabs?

If it does I don't think this line is correct

weapons[selected].transform.parent.position

avatar image NoseKills · Oct 22, 2014 at 06:30 PM 0
Share

The error you are getting should contain a line number to indicate exactly where the npe happens. From that it should be pretty simple to track back what is left unassigned...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by bubzy · Oct 22, 2014 at 09:09 PM

nowhere in that code have you initialised the weapons[] array.

at the very least you will need to do

GameObject[] weapons = new GameObject[10]; //or whatever your number of items is

and then you gotta put some stuff in there :D

Comment
Add comment · Show 3 · 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 topkekker · Oct 22, 2014 at 10:19 PM 0
Share

So i put that in my code, Now how do i add objects too it?

avatar image wijesijp · Oct 23, 2014 at 03:24 AM 0
Share

If it is a public array you plan to hold prefabs you don't have to declare the size

Go to the editor

select the component with the script

you should see weapons variable expand it

then give prefab count in size and drag prefabs to the open slots

avatar image richyrich · Oct 23, 2014 at 12:36 PM 0
Share

As an example of wijesijp's suggestion, the following code would also work without having to initialise in the script (if you set it up visually)...

 using UnityEngine;
 using System.Collections;
 
 public class TestByOnlySettingInInspector : $$anonymous$$onoBehaviour {
     public GameObject[] iSetThisArrayElementSizeAndObjectsInTheInspector;
     // Update is called once per frame
     void Update () {
         if (iSetThisArrayElementSizeAndObjectsInTheInspector != null)
         {
             if (iSetThisArrayElementSizeAndObjectsInTheInspector.Length > 0)
             {
                 for (int i = 0; i < iSetThisArrayElementSizeAndObjectsInTheInspector.Length; i++)
                 {
                     Debug.Log("I still work was called"); 
                     Debug.Log(iSetThisArrayElementSizeAndObjectsInTheInspector[i].name);
                 }
             }
         }
     }
 }
 

This does mean however you need to remember to setup via the inspector each time you add the script somewhere else. bubzy's suggestion means the array size is set for you, but you still then need to setup the GameObjects visually (as explained by wiksijp) or via script using:

 GameObject.Find("NameOfObjectInHierarchy");


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.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Inventory "Remove" Function Help 1 Answer

How to ignore base class? 1 Answer

Can't Assign Item In Array 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