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 Jan 11, 2016 at 03:32 PM by AnniJ for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by AnniJ · Jan 10, 2016 at 04:31 PM · c#script.getcomponentinventoryraycasthit

Cannot find a script compnent with raycast

Hello people :)

I need to find a script component from the items I click on but Unity refuses to find it no matter what I do :(

This is my script

 using UnityEngine;
 using System.Collections;
 
 public class Pickup : MonoBehaviour {
 
     Camera camera;
     Collider other;
     public Inventory inventory;
 
     void Start(){
         camera = GetComponent<Camera>();
     }
 
     void  Update (){
         int x = Screen.width / 2;
         int y = Screen.height / 2;
         RaycastHit hit;
         Ray ray = camera.ScreenPointToRay(new Vector3(x, y,0));
         Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
         
         
         if (Input.GetMouseButtonUp(0) && Physics.Raycast(ray, out hit, 50) && hit.collider.gameObject.tag == "item") {
                 print("Hit something");
                 Destroy(hit.transform.GetComponent<Collider>());
                 hit.transform.gameObject.active = false;
                 inventory.AddItem(other.GetComponent<Item>());
         }
     }
 }

And this is the error I get:

NullReferenceException: Object reference not set to an instance of an

object Pickup.Update () (at Assets/Standard Assets/InvScripts/Pickup.cs:26)

Does anyone know whats going on?

Thanks in advance ;)

Comment
Add comment · Show 1
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 dval · Jan 10, 2016 at 04:41 PM 0
Share

Is 'inventory' set in the inspector? That's what line 26 is looking for.

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by DominikP · Jan 11, 2016 at 03:29 PM

When do you asign "other" object? I think that you never asign this variable.

Try to do:

 inventory.AddItem(hit.transform.GetComponent<Item>()); 

or make first:

 other = hit.transform.GetComponent<Collider>();

Comment
Add comment · Show 1 · 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 AnniJ · Jan 11, 2016 at 03:32 PM 0
Share

Yes awesome that's it :D Totally forgot about the "other" thing^^ Thank you!! :D

avatar image
1

Answer by corn · Jan 10, 2016 at 04:39 PM

The NullReferenceException happens on that line :

 inventory.AddItem(other.GetComponent<Item>());

This means that inventory is null. This is because you didn't initalize it, you only declared it.

You just need to write :

 public Inventory inventory = new Inventory();

Unless it is a MonoBehaviour, in which case you'd have to go with :

 void Start()
 {
   camera = GetComponent<Camera>();
   inventory = GetComponent<Inventory>();
 }
Comment
Add comment · Show 1 · 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 AnniJ · Jan 11, 2016 at 11:24 AM 0
Share

Hi thanks :)

I tried it, but it still didn't initalize it. Then I just initialized it in the inspector which worked :D

But I still get this error:

NullReferenceException: Object reference not set to an instance of an object Pickup.Update () (at Assets/Standard Assets/InvScripts/Pickup.cs:26)

So I still think it's because of the "Item" component

Follow this Question

Answers Answers and Comments

57 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Using a variable value with GetComponent 1 Answer

Has my project's data corrupted? Script keeps returning null but 20 minutes ago it wasn't? 1 Answer

How can i use raycast hit to detect object by the object tag name ? 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