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 Flague · Aug 11, 2012 at 08:09 AM · transformlistdistancesort

Sort By Distance, Call it multiple times.

 using UnityEngine;

using System.Collections; using System.Collections.Generic;

public class TowerStone : MonoBehaviour {

public List targets; public GameObject projectile; private Transform target; private Transform myTransform; private float coolDown = 2; private float t; private delegate Transform myDelegate(Transform t1, Transform t2);

// Use this for initialization void Start () { myTransform = transform; float t = Time.time;

AddAllEnemies(); TargetEnemy(); }

// Update is called once per frame void Update () {

float c = Time.time - t; if (c > coolDown){ t = Time.time; Fire(); }

print(myTransform); //targets.RemoveAt(0); //print (c); }

void Fire(){ GameObject arrow = Instantiate (projectile, transform.position + new Vector3(0, 3, 0), Quaternion.identity) as GameObject; arrow.SendMessage("SetTarget", target);

}

public void AddAllEnemies(){ GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");

foreach(GameObject enemy in go){ AddTarget(enemy.transform);

} } private void SortTargetsByDistance(){

targets.Sort(delegate(Transform t1, Transform t2){ return Vector3.Distance(t1.position, transform.position).CompareTo(Vector3.Distance(t2.position, transform.position)); });

}

public void AddTarget(Transform enemy){ targets.Add(enemy); }

private void TargetEnemy(){ if (target == null){ SortTargetsByDistance(); target = targets[0]; } } private void TargetDestroyed(){ // i would clear the list here if i knew how to AddAllEnemies(); TargetEnemy(); } }

Hello I want to sort this list by it's distance to the gameObject, the first time you call it, it works perfectly. When an enemy dies i call it again and try to erase the list without success by the way, i know i am supposed to delete it via listname.removeAll(); but i dont know what to put inside even though i've tried reading it's document.

I know the problem is on the SortTargetsByDistance method that was taken from a tutorial, the second time I call it this error appears:

issingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.

I guess i destroy it when the first item on the list is destroyed but i dont know how to create it again or not destroy it, i took it from a tutorial and i dont understant it.

Bottom line i want a way to sort this enemys by it's distance and be able to do it over and over again everytime one dies, and also want to know how to clear one list, vie removeAll if possible.

Thanks to everyone who takes time reading this and trying to help me.

Comment
Add comment · Show 2
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 Fattie · Aug 11, 2012 at 09:58 AM 2
Share

click EDIT and format your code, use the "code" button (it has 1s and 0s on it)

avatar image Karsnen_2 · Jan 08, 2013 at 07:45 PM 0
Share

yes pls. Use the Code Button.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by zyzyx · Aug 11, 2012 at 11:05 AM

 somelist.Clear();

Clears the list.

Comment
Add comment · 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
0

Answer by freeaces · Feb 03, 2013 at 03:22 PM

If you destroy the dead enemy as an object, this would work fine as far as I know:

 List<float> distances = new List<float>();
         var go = GameObject.FindGameObjectsWithTag("Enemy");
         for (int i = 0; i < go.Length; i++)
         {
             
                 var distance = Vector3.Distance(transform.position, go[i].transform.position);
                 distances.Add(distance);
                 
             
             
         }
         int index = distances.IndexOf(distances.Min());
         TargetObj = go[index].transform;

 
Comment
Add comment · 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

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

11 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

Related Questions

Sorting list of Transforms 1 Answer

A node in a childnode? 1 Answer

Sort - OrderBy GameObjects by Distance 2 Answers

sort List alphabetically 2 Answers

List Sort not Returning Value 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