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 imp903 · Jun 14, 2015 at 12:42 PM · javascriptraycastweapons

Pickup Weapons with raycast? JS

Ok, so I have set up a system where when the player steps over an object, it raycasts down and if there's something there a gui is brought up and the player should be able to press e to pick up the weapon. But, every time i press e on the weapon I get this error:

NullReferenceException: Object reference not set to an instance of an object EquippedWeapons.Update () (at Assets/EquippedWeapons.js:38

I've attached a script called EquippedWeapons to the player who has a child that is the camera and listener which then has a child for all of the weapons in the game under that camera. The system I'm going with for now is to have every weapon available but can only be set to active if the player picks it up. WeaponPickup is the script I've attached to the weapon on the ground which contains the variable this weapon which is a game object. I defaulted weapon1 to be a pistol and weapon two is an assault rifle. the weapon I'm picking up currently is also a pistol which may be what's presenting problems because of the activating and deactivating in the hierarchy system I took for switching. The script should explain it better than I can, but my point is that every time I try to pick up a weapon it pauses and gives me that error message. I think there's some flaw in logic of some sort but I'm not sure. I'm not married to this system though of picking up weapons, so if anybody has a suggestion on a better overall strategy to handle the picking up and equipping of weapons I'd consider that as well.

 var weapon1 : GameObject;
 var weapon2: GameObject;
 var distance : float;
 
 function Start() {
     weapon1.SetActive (true);
     weapon2.SetActive (false);
 }
 
 function Update () {
     
     //changes active weapon
     if (Input.GetKeyDown(KeyCode.Tab)) {
         swapWeapons();
     }
     
     //pickup weapons scripting
     var hit : RaycastHit;
     
     //raycast to find pickup-able weapon, and displays appropriate GUI prompt if so
     if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.down), hit)){
         distance = hit.distance;
         if (distance <= 1.5) {
             hit.transform.SendMessage("showGui", SendMessageOptions.DontRequireReceiver);
             
             //sets weapon 1 or two equal to picked up weapon depending on which is active. Problem lies somewhere in here I believe
             if (Input.GetKeyDown(KeyCode.E))
             {
                 Debug.Log ("ButtonPressed");
                 if (weapon1.activeInHierarchy == true)
                 {
                     weapon1 = hit.transform.GetComponent.<WeaponPickup>().thisWeapon;
                 }
                 if (weapon2.activeInHierarchy == true)
                 {
                     weapon2 = hit.transform.GetComponent.<WeaponPickup>().thisWeapon;
                 }
             }
         }
     }
 }
 
 
 function swapWeapons ()
 {
     if (weapon1.activeInHierarchy == true) {
             weapon1.SetActive (false);
             weapon2.SetActive (true);
         } else {
         weapon1.SetActive(true);
         weapon2.SetActive(false);
         }    
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Cherno · Jun 14, 2015 at 02:34 PM

You are trying to access a component "WeaponPickup" of the hit gameObject which it doesn't have, or the "thisWeapon" GameObject variable of the component is null.

try checking if the required component and the weapon variable in it isn't null before trying to access it.

 WeaponPickup weaponPickup = hit.transform.GetComponent.<WeaponPickup>();
 if(weaponPickup != null) {
      GameObject thisWeapon = weaponPickup.thisWeapon;
      if(thisWeapon  != null) {
            weapon2 = thisWeapon  ;
      }
 }
 
 
Comment
Add comment · Show 2 · 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 imp903 · Jun 15, 2015 at 12:19 AM 0
Share

Thanks a ton, that fixed it perfectly!

avatar image Cherno · Jun 15, 2015 at 11:29 AM 0
Share

Glad to be of help.

You might find that using OnTriggerStay() is an easier way of pinteracting with items laying on the ground.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Clicking on an Object to Make it the Variable Target 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Raycast Pause Between Hits? 0 Answers

Changing position of a RayCast 1 Answer

Convert c# to Js? 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