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 18, 2014 at 01:35 AM · enemyspheretargetoverlapclosest

Closest target acquisition using Physics Overlap Sphere script not working

Hey guys, this is my first post so bear with me ^_^

I'm trying to create a target acquisition script using a physics overlap sphere mechanic. I'm very new to coding so this is the best I could do atm. I tried to set the turret to turn towards the closest target within the overlap sphere. However I keep running into a lot of errors and I managed to narrow it down to this.. but im fairly stumped! It tells me "Object reference not set to an instance of an object", however I am sure I set the beginning instance to null "closest".

Basically, my plan is to find all collisions within sphere, find the closest one's position then have my turret face towards that target.

Any help would be really appreciated!

 var damping = 6.0;
 var smooth = true;
 var attackRange = 50;
 
 var test = true;
 static var targetTag : String;
 
 var targets = GameObject;
 
  
 function Start()
 {
 //    InvokeRepeating ("FindClosestEnemyPlayer", 0, 1);
 }
  
 function Update()
 {  
     if (test == true)
     {    
         var hit : RaycastHit;
         var ray : Ray;
         var fwd = transform.TransformDirection (Vector3.forward);
         if (Physics.Raycast (transform.position, fwd, hit, attackRange))
         {
             if(hit.collider.name == "Player")
             {
                 print("target` hit!");
             }
         }    
         if (Physics.Raycast (transform.position, fwd, attackRange))
         {
         }
         Debug.DrawRay (transform.position, fwd * attackRange, Color.white);
     }
 
 //Script for rotating towards acquired target
     if (test == true)
     {
         if (smooth)
         {
             var target : Transform = FindClosestTarget();
             // Look at and dampen the rotation
             var rotation = Quaternion.LookRotation(target.position - transform.position);
             transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
         }
     }
 }
 
 
 function FindClosestTarget() : Transform
 {
     var closest : Transform = null;
     var origin : Vector3 = transform.position;
     var distance = Mathf.Infinity;
     var cols : Collider[] = Physics.OverlapSphere(origin, attackRange);
  
     for (var hit : Collider in cols)
     {
         var diff = (hit.position - origin);
         var curDistance = diff.sqrMagnitude;
         if (curDistance < distance)
         {
             distance = curDistance;
             closest = hit.transform;
         }
     }
     return closest;
 }
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 robertbu · Jul 18, 2014 at 01:36 AM 0
Share

Please edit your question and add a copy of the error message taken from the Console. It provided the line number of the error and the stack trace.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Jul 18, 2014 at 01:43 AM

I see one problem here. Your function findClosestTarget() returns null if Physics.OverlapSphere() does not find any colliders. But on line 43 you use target.position without checking if target is null. Line 8 is also weird. You set 'targets' to the class 'GameObject'. I think you want:

 var targets : GameObject;

I believe having #pragma strict at the top of the file would have found this issue at compile time. It is always a good idea to have '#pragma strict' at the top of the file.

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 19, 2014 at 09:06 AM 0
Share

I ended up not using the sphere but I kept the get closest enemy function and I created a script that allows my unit to turn towards the target.

However adding in the pragma really helped me a lot since it found out bugs I wouldnt have been able to find. Thanks for answering!! :)

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

overlap half sphere? 0 Answers

Is this the most efficient way to attack the closest enemy? 0 Answers

Sphere Overlap EachOther 0 Answers

Enemy fire script 1 Answer

Enemies overlapping each other 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