Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Jul 23, 2019 at 01:26 AM by DevelopmentCompanyOfEntertainingGames for the following reason:

It's only a basic problem of forgetting.

avatar image
1
Question by DevelopmentCompanyOfEntertainingGames · Jul 23, 2019 at 09:50 AM · errornull reference exception

Null Reference Exception

Hello, I have a problem. I'am making a RPG game so i need inventory, all work for the moment, except one thing. I create a function to equip an item :


 public void EquipItem()
     {
         if (itemObjects[itemObjects.Count - 1].item.Type != Item._Type.Consumable && itemObjects[itemObjects.Count - 1].item.Type != Item._Type.Quest)
         {
             equipment.EquipItem(itemObjects[itemObjects.Count - 1]);
         }
     }

This function use a list (for itemObjets), the conditions work, but not the result :(equipment.EquipItem(itemObjects[itemObjects.Count - 1]);)

The console show this error : alt text

(NB: the line 74 is : equipment.EquipItem(itemObjects[itemObjects.Count - 1]);) in the script

For me it's impossible because, the conditions work with the "index" of list : itemObjects.Count - 1, but why the "index" of list don't work on line 74 equipment.EquipItem(itemObjects[itemObjects.Count - 1]);

I need your help please!


Sorry for my bad English i'm French ^^

Thanks in advance.

Comment
Add comment · Show 4
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 Cornelis-de-Jager · Jul 23, 2019 at 12:28 AM 0
Share

Try to put print(itemObjects.Count) before the if statement and have a look at the count.

avatar image DevelopmentCompanyOfEntertainingGames Cornelis-de-Jager · Jul 23, 2019 at 12:51 AM 0
Share

Thank you for your ansew! :)


I did it, and the console show 1 So with the formula itemObjects.Count - 1 we get 0, therefore the first itemObject in the list itemObjects.

It should work, don't understand the problem. :'(


PS: the formula itemObjects.Count - 1 is for select the last itemObject in list.

avatar image Bunny83 · Jul 23, 2019 at 01:10 AM 0
Share

Plain debugging questions, especially about null reference exceptions are classified as too basic and off topic:

Posts that include a compiler error or NullReferenceException: all of these compiler errors (such as BCE0001) are included as tags, and there are multiple questions already. These new posts need to be tagged and then rejected.

A NullReferenceException is one of the most basic concepts of C# / .NET and nothing Unity specific. Apart from that your question does not contain enough information. A null ref exception is always the same thing. You have a variable that contains a null value and you try to access the object that is see$$anonymous$$gly behind that reference. In your code that means either the element in your "itemObjects" array is null, or your "equipment" variable is null, or the issue is actually inside your "EquipItem" method. We don't see your stacktrace so we can't say anything else about that.


Debugging is a major part of program$$anonymous$$g and you should be able to solve such basic issues yourself. The stacktrace of the error tells you the exact line. If you have too many statements in that single line you should seperate things into multiple lines.

avatar image DevelopmentCompanyOfEntertainingGames Bunny83 · Jul 23, 2019 at 01:25 AM 0
Share

Indeed, you are a thousand times right, in normal times I am able to fix these basic types of problems, the error was just to 'equipment' which the variable that is automatically assigned by a 'FindObjectOfType<>()' but I completely forgot to assign the script equipment component to a GameObject, thanks for your help, I will be careful next time and I will avoid bothering you with such basic problems.

I hope I've been able to make myself understood. Goodbye :)

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

140 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Space Shooter: If that impact but not explode or ship or asteroids. 3 Answers

NullReferenceException on TransformArray 1 Answer

Null Reference When Calling OnTriggerEnter? 2 Answers

Null Reference Exception Error 1 Answer

im getting this error: NullReferenceException: Object reference not set to an instance of an object Item.Start () (at Assets/Scripts/Inventory System/Item.js:9) 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