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 Kiino · Jul 21, 2014 at 04:34 AM · colliderplayerenemynullray

Null reference exception on raycast collider hit? Targetter vs Moving Player

Okay so I have this code that allows this unit to target the player. It uses a ray cast script to do this. However, when I create about 5 of these units and they target the player as I'm moving around I get a null reference exception. I think this is because sometimes the ray gets blocked by another unit or something. Either way I need to figure out the root of this problem.

The error points out this code...

 if (hit.collider.gameObject==targetObj && hit.collider.gameObject !== null)

here is the rest of the code...

 function Update()
 {
     var targetObj : GameObject = targetter;
     
     if(targetObj !== null)
     {
         var target : Transform = targetObj.GetComponent(Transform);
         
         //if an enemy as further than maxDistance from you, it cannot see you
         var rangeSquared = range * range;
         var rayDirection : Vector3 = target.position - transform.position;
         var enemyDirection : Vector3 = transform.TransformDirection(Vector3.forward);
         var angleDot = Vector3.Dot(rayDirection, enemyDirection);
         var playerInFrontOfEnemy = angleDot > 0.0;
         var playerCloseToEnemy =  rayDirection.sqrMagnitude < rangeSquared;
          
         if (playerInFrontOfEnemy && playerCloseToEnemy)
         { 
              inCombat = true;
             //by using a Raycast you make sure an enemy does not see you 
             //if there is a bulduing separating you from his view, for example
             //the enemy only sees you if it has you in open view
             var hit : RaycastHit;
             Physics.Raycast (transform.position,rayDirection, hit, range);
             if (hit.collider.gameObject==targetObj && hit.collider.gameObject !== null)
             {
                    inCombat = true;
                  // Look at and dampen the rotation
                 var rotation = Quaternion.LookRotation(target.position - transform.position);
                 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);    
             
                 //Once the unit is facing the target, this unit will fire
                 if(Time.time - lastFired > reloadTime && Vector3.Angle(transform.forward, target.transform.position - transform.position) < angle)
                 {
                        Fire();
                        lastFired = Time.time;
                 }
             } 
             else 
             {
                 //enemy doesn't see you
                }
            }
            else
            {
                inCombat = 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
2

Answer by robertbu · Jul 21, 2014 at 04:38 AM

The problem is that you are not checking the return value from the Physics.Raycast(). If the Raycast() fails, then hit will not have a valid collider, so your code on line 25 will generate a null reference. If the Raycast() succeeds, then the hit will contain a value game object, so the solution is to combine lines 24 and 25 into:

  if (Physics.Raycast (transform.position,rayDirection, hit, range) && (hit.collider.gameObject==targetObj))

For future questions, please include a copy of the error message from the Console. It gives us the line number of the error and the stack trace.

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 Kiino · Jul 21, 2014 at 05:44 AM 0
Share

omg that worked! thank you so much!!! and yes will do!

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

2 People are following this question.

avatar image avatar image

Related Questions

Lose health on collision 1 Answer

Problems with enemy attack script! 0 Answers

Enemy can't find weapon collider 0 Answers

NavMesh problem it does not respond corectly 0 Answers

Ray cast collider recognition and execution of a script attached to hit object 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