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

Seemingly simple function only works half the time.

Discussion in 'Scripting' started by jju1, Jun 24, 2016.

  1. jju1

    jju1

    Joined:
    Jun 22, 2016
    Posts:
    4
    When using this function it only works around half the time, although practically nothing changes it suddenly doesn't destroy anything at random times with no error messages. Due to me being relatively new to C# and Unity, I am clueless as to why. Please help, Thanks.
    Code (CSharp):
    1.     public void ClearObjects() {
    2.             List<GameObject> Objs = GameObject.FindGameObjectsWithTag ("Object").ToList ();
    3.             GameObject Current = Objs[0];
    4.             for(int i = 0; i < Objs.Count; i++){
    5.                 if(Objs[i].GetComponent<Obstacle>().Age < Current.GetComponent<Obstacle>().Age){
    6.                     Current = Objs [i];
    7.                 }
    8.             }
    9.             List<GameObject> Points = GameObject.FindGameObjectsWithTag ("Point").ToList ();
    10.             for(int i = 0; i < Points.Count; i++){
    11.                 if(Points[i].GetComponent<PointScript>().pos == Current.GetComponent<Obstacle>().Position){
    12.                     Points [i].GetComponent<PointScript> ().Occupied = 0;
    13.                 }
    14.             }
    15.             if(Current.GetComponent<OilContact>() != null){
    16.                 this.gameObject.GetComponent<DifficultyMaster> ().OilCur--;
    17.                 this.gameObject.GetComponent<DifficultyMaster> ().OilNum--;
    18.             } else {
    19.                 this.gameObject.GetComponent<DifficultyMaster> ().ObjCur--;
    20.                 this.gameObject.GetComponent<DifficultyMaster> ().ObjNum--;
    21.             }
    22.             GameObject.Destroy (Current);
    23.     }
     
  2. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    Try a debug.log to see if the function is being called at all. if it is then place a debug.log after each if statement to see where its getting bogged down.

    if it isn't getting called at all check to see how the function is being called. The problem might be there and not in the function at all.
     
  3. awest

    awest

    Joined:
    Jul 12, 2014
    Posts:
    8
    Not sure why it's not working. I don't see anything wrong there. It should always destroy one object as long as there is one tagged "Object". Tags are case sensitive so keep that in mind.

    I do see a couple other things you might want to address depending on how often this gets called.

    Both GameObject.Find and GameIbject.GetComponent are expensive calls (more so the former) and should be used as little as possible.
    I might suggest storing the components you need in lists or at least keeping the list of objects and sort the new entry when an object is created. That way you aren't finding and sorting the same objects over and over again. Depending on how age works in game you may even want to use a Queue (in place of a List) which has automatic first in first out.