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 /
avatar image
0
Question by zak666 · Sep 13, 2016 at 07:50 PM · raycastscriptingbasics

Recast hit get component: NullReferenceException: Object reference not set to an instance of an object

Ok everything is set up on both objects and assigned in the inspector no idea why Im getting this: NullReferenceException: Object reference not set to an instance of an object

Player: // SeconderyWeaponLockon____

         if (Input.GetButtonUp ("LockOn")) {  // TOGGLES LOCK ON TO BE ACTIVE OR NOT ACTIVE
         
             if (LockedOnActive == false) {
                 LockedOnActive = true;
             } else {
                 LockedOnActive = false;
                 Crosshair1.SetActive (true);
                 Crosshair2.SetActive (false);
             }
         
         }
 
 
         if (LockedOnActive == true) {  // if lock on is active change crosshairs and activate raycast.
             Vector3 fwd = transform.TransformDirection(Vector3.forward); //the direction the player is facing
             Crosshair1.SetActive (false);
             Crosshair2.SetActive (true);
             RaycastHit hit = new RaycastHit ();
             if (Physics.Raycast (transform.position, fwd, 60000))
             if (Hit.collider.tag == "Red_Team") {
                 Hit.transform.gameObject.GetComponent<LockedOn>().IsLockedon = true;
                 LockedOnTarget = Hit.collider.gameObject;
             }
         }
             
         //_____________________________________________________________________________________________________________





Object Hit Script:

 using UnityEngine;
 using System.Collections;
 
 public class LockedOn : MonoBehaviour {
 
     // put this on all Enemy Ships and Targets that can be targeted
 
     public bool IsLockedon;
     public GameObject LockedOnSymbol;
 
 
     // Update is called once per frame
     void Update () {
         if (IsLockedon == true) {
             LockedOnSymbol.SetActive (true);
         }
     }
 
 }


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 AurimasBlazulionis · Sep 13, 2016 at 08:02 PM

First of all. You wrote Hit 2 times. Did you mean hit?

Second. Raycast could have hit a child object so you would need to change Hit.collider.tag with hit.transform.root.tag, Hit.transform.gameObject... with hit.transform.root.gameObject... and Hit.collider.gameObject with hit.transform.root.gameObject to make sure you check everything with the root gameObject in hierarchy.

The solution.

You did not set hit to anything. How can unity know to which value put all that information? Well, you did not tell so you should fix it. Put out hit as the argument before maximum distance. Now you should be good to go!

So the line looks like if (Physics.Raycast (transform.position, fwd, out hit, 60000))

Comment
Add comment · Show 4 · 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 zak666 · Sep 13, 2016 at 08:16 PM 0
Share

Everything in the hirachy is correct and tagged correctly changed to:

     if (LockedOnActive == true) {  // if lock on is active change crosshairs and activate raycast.
             Vector3 fwd = transform.TransformDirection(Vector3.forward); //the direction the player is facing
             Crosshair1.SetActive (false);
             Crosshair2.SetActive (true);
             RaycastHit hit = new RaycastHit ();
             if (Physics.Raycast (transform.position, fwd, 60000))
             if (hit.transform.root.tag == "Red_Team") {
                 hit.transform.root.gameObject.GetComponent<LockedOn>().IsLockedon = true;
                 LockedOnTarget = hit.transform.root.gameObject;
             }
         }


tho i am still getting null reference message Im storing the hit.GameObject so when the player fires a missile i can send that information to the missile so it locks on.

avatar image AurimasBlazulionis zak666 · Sep 13, 2016 at 08:21 PM 0
Share

Could you write the line which gets the null reference exception? So I could get the idea what's wrong

avatar image zak666 AurimasBlazulionis · Sep 13, 2016 at 08:27 PM 0
Share

if (hit.transform.root.tag == "Red_Team") {

tho my game object has the correct tag or Red_Team and has a box collider and rigid body so i don't know why its not picking it up.

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

73 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

Related Questions

GUI showUP when Raycasting 0 Answers

in rewarded ad which code means user closed the ad by itself 2 Answers

RayCast to test if player is grounded 1 Answer

Turn in the direction of movement 0 Answers

need help on raycast 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