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 /
  • Help Room /
avatar image
0
Question by AedanCGraves · Jul 29, 2016 at 11:01 PM · c#arrayscompare

Getting the closest GameObject to the player from an array

as the question implies... I need to get a GameObject from an array that's closest to the player so it can be destroyed.

Current Code (c#):

 public GameObject[] myArray;

 void Update(){
     myArray = GameObject.FindGameObjectsWithTag("randomTag").OrderBy(go => go.transform.position.magnitude).ToArray();
 }
 
 void OnTriggerStay (Collider col) {
 if (col.tag == "randomTag" && Input.GetButtonDown("randomButton")) {
         Destroy(myArray[0]);
       
     }
 }



Quite Honestly, I have now Idea how to go about this... I don't know if it needs to be done in defining the array order (maybe like comparing the Vector3 magnitude with the player's Vector3 magnitude, the only reason I haven't tried that is because I don't know how) Or some ting like that.

Any how, any help would be greatly appreciated.

thx m8s

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
0

Answer by Firedan1176 · Jul 30, 2016 at 12:00 AM

 void Update() {
     myArray = GameObject.FindGameObjectsWithTag("randomTag");
 }

 void OnTriggerStay(Collider col) {
     if(col.transform.CompareTag("randomTag") && Input.GetButtonDown(0)) {
         GameObject closest = null;
         foreach(GameObject g in myArray) {
             if(Vector3.Distance(transform.position, g.transform.position) < Vector3.Distance(transform.position, closest.transform.position) || closest == null)
                 closest = g;
         }
         if(g == null) Debug.Log("No enemies have the tag were found.");
         else Destroy(g);
     }
 }


Let me know how this works

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 AedanCGraves · Jul 30, 2016 at 12:52 AM 0
Share

Thanks! I don't have time at the studio to test it tonight, but just wanted to thank you for the quick response.

avatar image Firedan1176 AedanCGraves · Jul 30, 2016 at 07:28 PM 1
Share

No problem. Let me know if it works. If this helped, please accept an answer. It adds to my self esteem and pride. :)

avatar image AedanCGraves Firedan1176 · Jul 30, 2016 at 08:29 PM 0
Share

So, I tried it and assigned the proper tags and arrays and I trigger the event get an error saying: NullReferenceException: Object reference not set to an instance of an object

I feel like their should be an obvious answer to this, but as you probably guessed, I am still learning.

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

209 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 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 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

compare string to string array[] 2 Answers

Array of Arrays 3 Answers

Randomize text position for 2D Quiz C# 0 Answers

Need help comparing arrays/lists to show items that are missing from one of them 2 Answers

How do you assign a material to multiple different objects using arrays? 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