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 Irin · May 28, 2014 at 07:07 AM · c#raycastnullreferenceexceptionpickup

Picking Weapons Up with Raycast

Hello, i decided to write my own pick up weapons script using a raycast route. The logic is as follows:

  1. Tag a weapon with a new tag named after the weapon.

  2. Place the weapon prefab in the PickUpWeapon Script.

  3. When the PickUpWeapon script raycast hits any of the tagged weapons, and if the player presses E, then a class from Change Weapon Scrip is instantiated, and the PickedUpWeapon is put in the slot.

Unfortunately im getting a Null reference error. At the following line: cw.weapon[2] = AK; The value of Ak is the prefab assigned in this script, so logically when i call the change weapon script, the Ak should occupy slot 2.

 public class PickThingsUp : MonoBehaviour 
 {
     RaycastHit rayHit = new RaycastHit();
     Ray detect = new Ray();
 
     Vector3 Location;
 
     public string ak;
     public GameObject AK;
 
     // Use this for initialization
     void Start () 
     {
 
     }
     
     // Update is called once per frame
     void Update () 
     {
         detect = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
             ChangeWeapon cw = GetComponent<ChangeWeapon>();
             
             if(Physics.Raycast(detect, out rayHit, 150.0f))
             {
                 Location = rayHit.point;
                 
                 if(rayHit.collider.CompareTag("AK47"))
                 {
                     ak = "AK47";
                     
                     print ("its gunz");
 
                     if(Input.GetKey(KeyCode.E))
                     {
                         
                         cw.weapon[2] = AK;
                         cw.weapon[2].gameObject.SetActive(false);
                         print ("weapon achived");
                     }
                   
                 }
             }
     
     }
 }
 
 
 public class ChangeWeapon : MonoBehaviour 
 {
 
     public int currentWeapon;//current one used
     public GameObject[] weapon;
 
     // Use this for initialization
     void Start () 
     {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         Vector3 temp = transform.position;
 
         if(Input.GetKeyDown(KeyCode.Alpha1))
         {
             changeWeapon(1);
         }
         if(Input.GetKeyDown(KeyCode.Alpha2))
         {
             changeWeapon(2);
         }
     }
 
     public void changeWeapon(int num)
     {
         currentWeapon = num;
 
         for(int i = 1; i < weapon.Length; i++)
         {
             if(i == num)
                 weapon[i].gameObject.SetActive(true);
             else
                 weapon[i].gameObject.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 HarshadK · May 28, 2014 at 07:49 AM

As you've said that

"The value of Ak is the prefab assigned in this script"

I don't see where you are assigning the prefab to AK in your script. And since in your PickThingsUp script the AK is not assigned with any prefab/game object it is throwing the null reference error.

Comment
Add comment · Show 7 · 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 Irin · May 28, 2014 at 04:58 PM 0
Share

I assigned the Ak value in the inspector property.alt text

issues.png (17.6 kB)
avatar image HarshadK · May 28, 2014 at 05:40 PM 0
Share

What is the size of your weapon array?

avatar image Irin · May 29, 2014 at 04:36 AM 0
Share

The weapon array size is changed in the ChangeWeapon Script; as needed, currently i have it set to: 3. Element 0 is empty on purpose, element 1 is a revolver, the Ak should go in element 2. Is it possible that when i said element 2 i should have specified 3 or is it on, since the array starts at 0?

avatar image HarshadK · May 29, 2014 at 05:42 AM 0
Share

Got it! Since you are accessing the ChangeWeapon script like this:

 ChangeWeapon cw = GetComponent<ChangeWeapon>();

It requires you to have the ChangeWeapon script on the PickThingsUp game object itself. But as per the screenshot you provided above it is not there.

If you have placed ChangeWeapon on some other game object then you need to access it like:

 ChangeWeapon cw = GameObject.Find("AnotherGameObject").GetComponent<ChangeWeapon>();

Here AnotherGameObject is the name of the game object to which the ChangeWeapon script is attached.

If you have not assigned ChangeWeapon Script to any game object then you need to do so.

avatar image Irin · May 30, 2014 at 04:04 AM 0
Share

Yes :D! it kind of worked, i have no null error. It tells me the weapon has been achieved, but... when i press 2 to get to the Ak, it does not show up. I think, this is because i have no transform position linked. Um... here is a screen shot, Any thoughts on which script and how i should place the weaponsObject transform position?alt text

issue2.png (49.1 kB)
Show more comments

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

20 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

Related Questions

raycasting in c# - NullRefrenceException error 2 Answers

AI raycasting problem 0 Answers

Object reference not set to an instance of an object 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers


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