Search Unity

Object finder code not working help Please|

Discussion in 'Scripting' started by Epic-Username, May 24, 2015.

  1. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    I cant work out why this is not working i have tried researching it but couldn't find any answers.

    Code (CSharp):
    1.     void Update () {
    2.         transform.position = Vector3.MoveTowards(transform.position, object1.transform.position, speed);
    3.     }
    4.  
    5. void findTarget(){
    6.         objects1 = GameObject.FindGameObjectsWithTag ("object1");
    7.         for(int i = 0; i <objects1.Length;i++){
    8.  
    9.             if(Vector3.Distance(objects1[i].transform.position,transform.position) > Vector3.Distance(objects1[i].transform.position,nearestTarget.transform.position)){
    10.                 nearestTarget =objects1[i];
    11.                object1 = objects1[i];
    12.         }
    13.     }
    14.  
    15. }
    what is happening is the object is shooting towards the position 0,0,0 when no object is there. Can you please explain why this is happening and the right code thanks :)
     
  2. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Would you please post all of your code?
    You're not calling findTaget() in this snippet. Plus it is unclear where object1 comes from and what its value is.
     
    krougeau likes this.
  3. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    As Duugu says, further information might help. Given what you've shown us, however, I'd suggest trying something like the following code, which will "findTarget" when object1 hasn't been located, and will "transform.position" only when object1 has been found. Untested, mileage may vary...
    Code (CSharp):
    1.     void Update ()
    2.     {
    3.         if(object1 == null)
    4.         {
    5.             findTarget();
    6.         }
    7.         if(object1 != null)
    8.         {
    9.             transform.position = Vector3.MoveTowards(transform.position, object1.transform.position, speed);
    10.         }
    11.     }
    12.    
    13.     void findTarget()
    14.     {
    15.         objects1 = GameObject.FindGameObjectsWithTag ("object1");
    16.         for(int i = 0; i <objects1.Length;i++)
    17.         {  
    18.             if(Vector3.Distance(objects1[i].transform.position,transform.position) > Vector3.Distance(objects1[i].transform.position,nearestTarget.transform.position))
    19.             {
    20.                 nearestTarget = objects1[i];
    21.                 object1 = objects1[i];
    22.             }
    23.         }
    24.     }
     
  4. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class enemys : MonoBehaviour {
    6.  
    7.     private float speed = 1.5f;
    8.     public GameObject object1;
    9.     private GameObject[] objects1;
    10.     private GameObject nearestTarget;
    11.  
    12.     void Start() {
    13.         nearestTarget = GameObject.FindWithTag ("object");
    14.     }
    15.     void Update () {
    16.         if (object1 == null) {
    17.             findTarget ();
    18.         }
    19.         if (object1 != null) {
    20.             transform.position = Vector3.MoveTowards (transform.position, object1.transform.position, speed);
    21.                }
    22.     }
    23.     void OnTriggerEnter(Collider other)
    24.     {
    25.         if (other.gameObject.CompareTag ("object"))
    26.         {
    27.             findTarget();
    28.             Destroy(other.gameObject);
    29.         }
    30.     }
    31.  
    32.     void findTarget(){
    33.         objects1 = GameObject.FindGameObjectsWithTag ("object");
    34.         for(int i = 0; i < objects1.Length;i++){
    35.  
    36.             if(Vector3.Distance(objects1[i].transform.position,transform.position) > Vector3.Distance(objects1[i].transform.position,nearestTarget.transform.position)){
    37.                 nearestTarget = objects1[i];
    38.                 object1 = objects1[i];
    39.             }
    40.             }
    41.     }
    42.  
    43. }
    Full code but i renamed some variables to object1 and objects1[].
    I want it to find nearest object with the tag "object", But it keeps going towards 0,0,0 when there's nothing there.
     
  5. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    OK, looking at the whole picture, I see some logic errors. The main biggie is that you're setting up the nearestTarget from Start without really knowing what the actual nearest target is... This led to some other issues, so I pretty much scrapped your code and put this together for you based on a combination of what you had & the 2nd example on the FindGameObjectsWithTag example in the documentation... Anyway, it's tested and works fine on my end, so it should work for you as well. Try this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class enemys : MonoBehaviour {
    5.        
    6.     private float speed = 1.5f;
    7.     public GameObject object1;
    8.     private GameObject[] objects1;
    9.     private GameObject nearestTarget;
    10.     private float distance;
    11.     private float curDistance;
    12.     private Vector3 pos;
    13.     private Vector3 diff;
    14.  
    15.     void Start()
    16.     {
    17.         nearestTarget = null;
    18.         distance = Mathf.Infinity;
    19.         pos = transform.position;
    20.         objects1 = GameObject.FindGameObjectsWithTag("object");
    21.         FindTarget();
    22.     }
    23.  
    24.     void Update ()
    25.     {
    26.         if (object1 != null)
    27.         {
    28.             transform.position = Vector3.MoveTowards (transform.position, object1.transform.position, speed);
    29.         }
    30.     }
    31.  
    32.     void OnTriggerEnter(Collider other)
    33.     {
    34.         if (other.gameObject.CompareTag ("object"))
    35.         {
    36.             Destroy(other.gameObject);
    37.             FindTarget();
    38.         }
    39.     }
    40.  
    41.     void FindTarget()
    42.     {
    43.         foreach (GameObject obj in objects1)
    44.         {
    45.             diff = obj.transform.position - pos;
    46.             curDistance = diff.sqrMagnitude;
    47.             if(curDistance < distance)
    48.             {
    49.                 nearestTarget = obj;
    50.                 object1 = nearestTarget;
    51.                 distance = curDistance;
    52.             }
    53.         }
    54.     }
    55. }
    I did reformat some stuff to suit me, haha. Just OCD like that :p
     
  6. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339

    When i attach this code to the gameobject it only goes to first object then stops it doesent go to any others. Would it make a difference that i'm cloning the objects in a spawner object?
     
  7. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    I was playing around with it for a bit trying to solve the problem and i found out that when it find the object it sets that to object1. Then once it goes to it and deletes the object it doesn't set another one for some reason. I wouldn't know how to fix it because the code looks right to me.
     
  8. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Yeah, I didn't take the "moving on" portion into account and am testing things out now. Hopefully I can come up with a fix for you soon :)
     
  9. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    BTW is this fps efficient in large numbers because the spawner will probably make up to 1000 clones and there will be about 20 different objects tracking all the clones.
     
  10. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    So far it's as efficient as I know how to make it, though I am still fairly new to code optimization. That aside, I'm having no luck at all moving the primary gameobject on to the next "object" in the array. I've tried arrays and lists to no avail. No matter what I try, I can't seem to properly clear and recreate either the array or list (thought lists would be easier, but no such luck) in such a way as to have the code iterate back through the remaining "object" tagged items in the scene. I've been digging around for an hour or more now, looked over several examples & documentation pages, and am still coming up empty handed.
     
  11. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    there must be a solution to this though because im sure other people have done this.
     
  12. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    I don't get why your code is not working though because if you call findTarget it should find another object i don't get this.
     
  13. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Well, the main issue stems from the fact that we're deleting the closest "object" tagged item, but that creates a "Missing" game object reference in our array &/or list. The code won't iterate through the list any further because of this and so it just sits there looking stupid :p I'm pulling my hair out trying to come up with a workable solution... It would be a bit easier if we always knew just where in the array/list an item sat, but since we're just looking for whichever one is closest, it can be any position in the array/list. I've tried every applicable trick I could find and am still throwing things at it to see what sticks, but so far I'm getting nowhere (unless you count the headache that's brewing).
     
  14. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    This one irritates me the most... Looks simple enough, right? Doesn't work...
    http://docs.unity3d.com/ScriptReference/Array.Clear.html

    Other versions I've come across say to try it like
    Code (CSharp):
    1. objects1.Clear(objects1, 0, objects1.Length);
    but that doesn't work...

    That's when I decided Lists might be a better choice... Turned the whole mess into a list and tried a few different ways with little or no success. The best I was able to do was have the List repopulate with the correct existing items, only to have Unity tell me that I couldn't work with it anymore because it had changed... *sigh*
     
  15. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    when i do programming in java with eclipse i use something called a Linked List to store my game objects where its like an array but you cant control location of objects in the array you can only add and remove objects to the list is there a thing similar to that in unity
     
  16. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Yeah, Lists... Not having any luck there either, but take a look at the vid &/or documentation, perhaps you'll come across something I haven't tried...

    It really shouldn't be this difficult, I'm just missing something obvious, I'm sure.
     
  17. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    by the time we find the answer we would have gone through so much stuff that we would be experts.
     
    Last edited: May 25, 2015
  18. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    It's driving me nuts, and I don't know what else to do, so I put out a call for backup, haha. Hopefully someone can explain to me just what I'm doing wrong and how to go about it correctly. Started a new thread here to address this particular issue: http://forum.unity3d.com/threads/fi...array-destroy-it-move-to-next-nearest.328061/
     
  19. Nitugard

    Nitugard

    Joined:
    May 10, 2015
    Posts:
    343
    I just answered this is another thread, still I don't see a reason why would you create new one for this issue?
     
    krougeau likes this.
  20. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    I created the new thread to answer my question, which just happened to be the solution to the OP's issue. After wracking my brain for hours and digging through various examples to no avail, I thought I'd ask for my own edification. Thank you again for answering. I'll be going over the code shortly and learning from it for my own future reference.
     
  21. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Special thanks to OneDragutin for the following, functional code & to Kragh for additional support:

    Code (CSharp):
    1.     using UnityEngine;
    2.     using System.Collections;
    3.     using System.Collections.Generic;
    4.     using System.Linq;
    5.    
    6.     public class PlayerController : MonoBehaviour
    7.     {
    8.    
    9.         private Vector3 pos;
    10.         private float speed = 1.5f;
    11.         public GameObject object1;
    12.         public List<GameObject> objects1 = new List<GameObject>();
    13.    
    14.         void Start()
    15.         {
    16.             pos = transform.position;
    17.             objects1 = GameObject.FindGameObjectsWithTag("object").ToList();
    18.             FindTarget();
    19.         }
    20.    
    21.         void Update()
    22.         {
    23.             if (object1 != null)
    24.             {
    25.                 transform.position = Vector3.MoveTowards(transform.position, object1.transform.position, speed);
    26.             }
    27.         }
    28.    
    29.         void OnTriggerEnter(Collider other)
    30.         {
    31.             if (other.gameObject.CompareTag("object"))
    32.             {
    33.                 objects1.Remove(other.gameObject);
    34.                 Destroy(other.gameObject);
    35.                 FindTarget();
    36.             }
    37.         }
    38.    
    39.         void FindTarget()
    40.         {
    41.    
    42.             float lowestDist = Mathf.Infinity;
    43.    
    44.    
    45.             for(int i=0; i<objects1.Count; i++)
    46.             {
    47.    
    48.                 float dist = Vector3.Distance(objects1[i].transform.position, transform.position);
    49.    
    50.                 if (dist<lowestDist)
    51.                 {
    52.                     lowestDist = dist;
    53.                     object1 = objects1[i];
    54.                 }
    55.            
    56.             }
    57.         }
    58.     }
    59.  
    Works like a charm!
     
    Nitugard likes this.
  22. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    hate to be negative but this doesn't work how i want it. This goes towards objects in order of first added to last not to closest one like i want and how do i update the list because i made a spawner in another script how do i change this list from another script
     
  23. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Well, it's at least a step in the right direction, correct? I mean, c'mon, when we started you couldn't get it to do anything but move to (0,0,0), right? :p
     
  24. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Strange, I just tested it with my little sample scene, and it does in fact move through the list based on how far the objects are, not just through the list from start to finish...

    Update: Added a wider spectrum of test objects and scattered them all over the place. The code works just the way you'd asked for it to. It moves to the nearest object, destroys it on contact, then moves to the next nearest object, and so on and so forth... Perhaps the issue is in fact with how you're spawning the objects? Mine are static, so they stay wherever they were when the calculations were initially made.
     
    Last edited: May 25, 2015
  25. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    i think i found the problem since i have multiple objects with this script they all rush to the closest one and if whatever one gets to it first deletes it all the other objects crash trying to look for an object that's already been deleated
     
  26. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Oh yeah, I can see where that might cause some issues. Now you just need to work out a way to have the deleted objects inform the searchers that they're no longer there so that the searching entities can update their lists. Perhaps set up an Event and Event Listeners? Just a thought. My brain is tired, lol :p
     
  27. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    I never knew such a small thing like this can get so complex i'm not used to unity that's probably why, I could code this in pure java that takes less then the time it took to figure out this.
     
  28. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    I think my own inexperience plays a large role in why this took so long as well. I've certainly learned a lot recently, but I've still got a long way to go & lots to learn, too.. Part of the reason I pop onto these forums is to hone my own skills by trying to tackle problems I might not otherwise face in my own coding. I just hope I'm more help than hindrance, haha!
     
  29. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    and last question do you now how to change a value with a script to all objects that have that script so i can do remove(object) to all that have the script because when i do remove(object) i only removes it from that objects script not all others objects with the script
     
  30. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    Thanks for your help to everybody that contributed to my question i have learned a lot today
     
    krougeau likes this.
  31. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    There are a couple of ways, but I think the most elegant would be to set up Events and Event Listeners. Here's a link to the Unity video covering the topic: https://unity3d.com/learn/tutorials/modules/intermediate/scripting/events
    I've only recently started using them myself, but they're pretty powerful and versatile.
     
  32. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    Event Listeners aren't working for some reason
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Linq;
    6.  
    7. public class test : MonoBehaviour
    8. {
    9.  
    10.     public delegate void remove();
    11.     public static event remove OnTrigger;
    12.    
    13.     private Vector3 pos;
    14.     private float speed = 1.5f;
    15.     public GameObject object1;
    16.     private GameObject Other;
    17.     public List<GameObject> objects1 = new List<GameObject>();
    18.  
    19.     void OnEnable()
    20.     {
    21.         test.OnTrigger += Triggered;
    22.         pelletCreater.OnCreate += AddPellet;
    23.     }
    24.    
    25.    
    26.     void OnDisable()
    27.     {
    28.         test.OnTrigger -= Triggered;
    29.         pelletCreater.OnCreate -= AddPellet;
    30.     }
    31.  
    32.     void Start()
    33.     {
    34.         pos = transform.position;
    35.         objects1 = GameObject.FindGameObjectsWithTag("object").ToList();
    36.         FindTarget();
    37.     }
    38.    
    39.     void Update()
    40.     {
    41.         objects1 = GameObject.FindGameObjectsWithTag("object").ToList();
    42.         if (object1 != null)
    43.         {
    44.             transform.position = Vector3.MoveTowards(transform.position, object1.transform.position, speed);
    45.         }
    46.     }
    47.    
    48.     void OnTriggerEnter(Collider other)
    49.     {
    50.         if (other.gameObject.CompareTag("object"))
    51.         {
    52.             Other = other.gameObject;
    53.             if (OnTrigger != null) {
    54.                 OnTrigger ();
    55.             }
    56.             Destroy(other.gameObject);
    57.             FindTarget();
    58.         }
    59.     }
    60.     void Triggered()
    61.     {
    62.         objects1.Remove(Other);
    63.     }
    64.  
    65.     void Addobject()
    66.     {
    67.         GameObject sn = gameObject.GetComponent<objectCreater> ().objectCreated;
    68.         objects1.Add (sn);
    69.     }
    70.    
    71.     void FindTarget()
    72.     {
    73.        
    74.         float lowestDist = Mathf.Infinity;
    75.        
    76.        
    77.         for(int i=0; i<objects1.Count; i++)
    78.         {
    79.            
    80.             float dist = Vector3.Distance(objects1[i].transform.position, transform.position);
    81.            
    82.             if (dist<lowestDist)
    83.             {
    84.                 lowestDist = dist;
    85.                 object1 = objects1[i];
    86.             }
    87.            
    88.         }
    89.     }
    90. }
    91.  
    92.  
     
    Last edited: May 25, 2015
  33. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    I'll be happy to try and work something up for you later on, but at the moment my brain's are mush and I'm gonna need to lie my tired, grumpy self down :p
     
  34. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    solution yet?
     
  35. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Sorry, not as yet. Big storm outside woke me up or I wouldn't even be on yet, haha. My better half's asleep behind me and she'd kill me if I started watching the tutorial I'm going to need to see before I can help you with the Events setup. She'll likely be up in the next two hours or so though, and I'll look into it then.
     
  36. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    This looks like you're setting the Event manager and Event Listeners all in the same script. If my limited understanding is correct, you should have the Events happening on your "object" items, and the Event Listeners set up on your player, or whatever is seeking / destroying them. Again, I'll be able to assist further later.
     
  37. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    but i want event manager and listener in same script because the event happens in the script and it has to manage it in the script
     
  38. Nitugard

    Nitugard

    Joined:
    May 10, 2015
    Posts:
    343
    Find all game object of type 'yourScript' then put them in array and for each object in array call specific functiion.
     
    krougeau likes this.
  39. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    My apologies. Power went out so I gave up and went back to sleep :p I also misunderstood the intent of what you were trying to do. I assumed that, when one of your "object" items was removed, you want it to broadcast an event message and let all of the searcher objects know to update their lists. Now I see that you want one copy of your script to inform all of the other copies of that script to update their lists. Close enough to the same idea :) As OneDragutin states, the easiest thing to do would be to create an array or list of all of these scripts and then use a for each loop to call the needed function(s) on each of those scripts.

    If you need help implementing the code, just let me know and I'll see what I can whip together.
     
    Last edited: May 26, 2015
    Nitugard likes this.
  40. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    I don't think i know how to create a array of scripts and force them to call functions from that list can you show me an example code please.
     
  41. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Certainly. You'll probably want to use FindObjectsOfType since we're trying to access all copies of a certain script. Since your example script above is called "test", I've based the following code off of that. Haven't tested, but I think I've got it right, haha. Let me know how it goes.
    Code (CSharp):
    1.         test[] tests = FindObjectsOfType(typeof(test));
    2.         foreach (test e in tests)
    3.         {
    4.             // call whatever function(s) you need here.
    5.         }
     
  42. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    Would i need to put this array in the script that needs to be updated or can i make another script for it.

    also getting Assets/scripts/Handler.cs(11,24): error CS0266: Cannot implicitly convert type `UnityEngine.Object[]' to `test[]'. An explicit conversion exists (are you missing a cast?)
     
  43. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Since you've already set your events and everything up in this script, and since this is also presumably the script we're talking to and from, I'd just go ahead and work it into this one. I suppose either way would work, but I don't see any real need or point in creating an additional script. Granted, you know you're situation better than I do, haha, so do whatever suits you so long as you can get it to work :)

    as for the error, try changing it to
    Code (CSharp):
    1. test[] tests = GameObject.FindObjectsOfType(typeof(test));
    and see if that helps. If not, let me know and I'll tinker with it for ya until it does :p

    It may well need a variable declared for it to work. I'm fiddling with some similar stuff myself (setting up singletons), so it'll be easy enough for me to test if you need.
     
  44. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    still get error

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Handler : MonoBehaviour {
    5.    
    6.     void Start () {
    7.    
    8.     }
    9.  
    10.     void Update () {
    11.         test[] tests = GameObject.FindObjectsOfType(typeof(test));
    12.         foreach (test e in tests)
    13.         {
    14.             // call whatever function(s) you need here.
    15.         }
    16.     }
    17. }
    18.  
     
  45. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    OK, first things first, is there some reason you're calling this every frame in your script? Seems a bit excessive to me... Still, it's your game, haha ;) Anyway, here's a sample script that I've gotten working just fine (based on the initial script I helped you with before you added the Events). Tested & it works fine for me. Should give you a good starting point.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5.  
    6. public class FindTest : MonoBehaviour
    7. {
    8.     private Vector3 pos;
    9.     private float speed = 0.05f;
    10.     public GameObject object1;
    11.     public List<GameObject> objects1 = new List<GameObject>();
    12.  
    13.     void Start()
    14.     {
    15.         pos = transform.position;
    16.         objects1 = GameObject.FindGameObjectsWithTag("object").ToList();
    17.         FindTarget();
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         if (object1 != null)
    23.         {
    24.             transform.position = Vector3.MoveTowards(transform.position, object1.transform.position, speed);
    25.         }
    26.     }
    27.  
    28.     void OnTriggerEnter2D(Collider2D other)
    29.     {
    30.         if (other.gameObject.CompareTag("object"))
    31.         {
    32.             FindTest[] seekers = FindObjectsOfType(typeof(FindTest)) as FindTest[];
    33.             foreach(FindTest seeker in seekers)
    34.             {
    35.                 seeker.GetComponent<FindTest>().RemoveDead(other.gameObject);
    36.             }
    37.             objects1.Remove(other.gameObject);
    38.             Destroy(other.gameObject);
    39.             FindTarget();
    40.         }
    41.     }
    42.  
    43.     void FindTarget()
    44.     {
    45.    
    46.         float lowestDist = Mathf.Infinity;
    47.    
    48.    
    49.         for(int i=0; i<objects1.Count; i++)
    50.         {
    51.        
    52.             float dist = Vector3.Distance(objects1[i].transform.position, transform.position);
    53.        
    54.             if (dist<lowestDist)
    55.             {
    56.                 lowestDist = dist;
    57.                 object1 = objects1[i];
    58.             }
    59.        
    60.         }
    61.     }
    62.  
    63.     public void RemoveDead(GameObject dead)
    64.     {
    65.         objects1.Remove(dead.gameObject);
    66.         Debug.Log(gameObject.name + " has removed " + dead.gameObject.name + " from it's list!");
    67.         FindTarget();
    68.     }
    69. }
    My code is called FindTest, so I'm looking for all occurrences of that script in my scene whenever an "object" is collided with. Then I iterate through the array and tell each FindTest script to run a remove function and remove the current "object" from their list of objects. Let me know how it goes :)

    Update: Added a FindTarget(); call at the end of the RemoveDead function to keep the little buggers from occasionally stalling out on me haha.
     
    Last edited: May 30, 2015
  46. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    I fought of another way around this, The problem is that there's multiple numbers of the object and the objects try to go towards closest points over the screen and if multiple objects target the same point one of them will get there first and destroy it leaving a null space in the objects array for the others, But instead of making the objects as the objects it heads towards what about making personal pinpoints it creates at the points for it to follow and only it can delete its own pinpoints, So basically changing the array to a pinpoint array would that work?
     
  47. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    I'm not sure what you mean, but sure, give it a whirl and see what happens... Again, I wasn't having any trouble with the above implementation, but I'm just screwing around with a demo scene made of 400+ "objects" and 5 or 6 "seekers". Only problem I encountered was that if the seekers got too close, they'd join up and head for the same target. Every time a target is destroyed, it's removed from all of the seeker lists, so no nulls to speak of. However you can get it to work for you though, that's cool :)
     
    Last edited: May 31, 2015
  48. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    With the thing im trying to do at the moment i need a script to change and get information in other scripts and im not good at linking scripts. Can you show me a link on a tutorial to this.
     
  49. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Sure, there are a ton of them out there :) I haven't got time to sift through them and see which ones are best, but here are a few to get you going.







     
  50. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    How do i Instantiate a gameobject as a child of another gameobject. I have tried multiple tutorials over the internet but none of them worked.