Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Object finder code not working help Please|

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

  1. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. GameObject myChild = Instantiate(myPrefab, Vector3.zero, Quaternion.identity) as GameObject;
    3. myChild.transform.parent = myParent.transform;
    4.  
    (anything with "my..." is just a variable name)
    etc.
     
    Last edited: Jun 1, 2015
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    This should be:

    Code (csharp):
    1. test[] tests = GameObject.FindObjectsOfType<test>();
    The non-generic version will return you an array of Object, while the generic one will give you a correctly typed array.
     
    krougeau likes this.
  3. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    Thanks for your help guys, I should be fine from here but if i need anymore help or get stuck ill just post here again.
     
  4. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451

    Ah, I see. Thanks for the correction. It's not a function I'd made use of as yet personally and I had only the documentation to guide me. Great to know better now :)
     
  5. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    this is so annoying nothing is working
     
  6. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    Im about to give up i have been at this simple script for weeks and nothing is working.
     
  7. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    I still don't know whats causing them to freeze im just guessing.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5.  
    6. public class test : 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.         float lowestDist = Mathf.Infinity;
    42.      
    43.      
    44.         for(int i=0; i<objects1.Count; i++)
    45.         {
    46.          
    47.             float dist = Vector3.Distance(objects1[i].transform.position, transform.position);
    48.          
    49.             if (dist<lowestDist)
    50.             {
    51.                 lowestDist = dist;
    52.                 object1 = objects1[i];
    53.             }
    54.          
    55.         }
    56.     }
    57. }
    Can someone at least identify whats going wrong with this for me to try fix.
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    Copy-pasted your script, put it in a scene. It works out of the box. The object with the script on will move to the closest of all objects tagged with "object", and destroy it, before it moves on to the next one. This will happen until all of them are destroyed.

    Note that the OnTriggerEnters has to happen. This means that the "object"'s needs to have a trigger collider, and the object with the test-script on it needs to have a collider (can be trigger or not trigger), and a rigidbody. This is important - two objects with just trigger colliders won't fire any collisions. The grid on the bottom of this page is really important.

    If you want the object to not be affected by physics, set the rigidbody to have gravity off, and kinematic on.
     
    krougeau likes this.
  9. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    i don't think you understand,It works but since multiple objects are being created the yall fight over them so when one of the objects gets their first all the others stop.
     
  10. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    I thought this was why we added the code that tells all of the "seeking" objects to update their target lists. I added that part and it's been working fine. When two seeker objects are headed for the same target, the first one that reaches the target destroys it, then all of the target lists are updated, and each seeker moves on to the next available target. I take it you weren't able to get that part working to your satisfaction?