I want to make game that looks like Rocket Mania, it almost done but something was wrong with the TriggerExit. and now i and my team was stuck and dont know how to continue this game.
Maybe same of you guys please help us to make this game out?

Dani AI

Generated

Two quick, concrete problems stand out in the code you posted (thanks for sharing, ; asked the right question about showing code):

  • Clearing the whole connection list on any exit will immediately wipe valid connections. Calling a global Clear() inside an exit handler kills state for every pipe instead of just the one that left.
  • The removal loop in the second script uses the wrong loop condition, so it never removes the intended range and leaves stale Check references.

Fixes to try (safe, minimal changes)

  • Don’t clear the whole list inside OnTriggerExit. Remove only the specific entry(s) that belong to the leaving pipe. Iterate backward when removing from a list to avoid index problems. Example pattern:
void OnTriggerExit2D(Collider2D other)
{
    if (!other.CompareTag("Balok")) return;

    GameObject parent = other.transform.parent != null ? other.transform.parent.gameObject : other.gameObject;
    var owner = ThisPipe.Check;
    if (owner == null) return;

    int idx = owner.Obstacle.IndexOf(parent);
    if (idx < 0) return;

    for (int i = owner.Obstacle.Count - 1; i >= idx; i--)
    {
        var g = owner.Obstacle[i];
        var p = g.GetComponent<PipaRotate>();
        if (p != null) p.Check = null;
        owner.Obstacle.RemoveAt(i);
    }
}
  • When replacing/destroying many pipes, iterate a copy of the list so you don’t mutate while enumerating:
var copy = new List<GameObject>(Obstacle);
foreach (var o in copy) {
    var pos = o.transform.position;
    Destroy(o);
    Instantiate(newObstacle[Random.Range(0,newObstacle.Length)], pos, Quaternion.identity);
}
Obstacle.Clear();

Debug checklist

  • Add Debug.Log in OnTriggerEnter/Stay/Exit to see which callbacks run and in what order.
  • Verify Rigidbody2D/collider setup (at least one object must have a Rigidbody2D and colliders must be triggers).
  • Avoid relying on OnTriggerExit when you destroy/deactivate colliders; explicitly clear references before destroying or schedule Destroy with a small delay.

Those three changes (stop global Clear(), fix the removal loop, and use a copy when replacing objects) should make the trigger/connection logic stable.

Recommended Answers

All 3 Replies

No code. No help... Post the relevant code where you are having a problem.

public bool isConnectAll = false;

public GameObject [] newObstacle;

public float speed = 0.001f;

int obj = 0;

Vector3 posObs;

private Transform posY;

public List<GameObject> Obstacle = new List<GameObject>();

void Start () {

}


void Update () {

    if (isConnectAll == true){
        for (obj = 0; obj < Obstacle.Count; obj++) {
            posObs = new Vector3 (Obstacle[obj].transform.position.x, Obstacle[obj].transform.position.y, Obstacle[obj].transform.position.z);
            Destroy(Obstacle[obj]);
            SpawnObs();
            //StartCoroutine(secondBetween(1));
        }
        Obstacle.Clear();
        isConnectAll = false;
    }


}

void OnTriggerEnter2D(Collider2D other){
    if (other.tag == "Balok"){
        Obstacle.Add(other.transform.parent.gameObject);
        other.transform.parent.GetComponent<PipaRotate>().Check = this;
    }
}

void OnTriggerExit2D(Collider2D other){
    if (other.tag == "Balok"){
        Obstacle.Remove(other.transform.parent.gameObject);
        other.transform.parent.GetComponent<PipaRotate>().Check = null;
        Obstacle.Clear();
    }
}

public void SpawnObs(){
    int i = Random.Range(0,newObstacle.Length);
    Instantiate (newObstacle[i], posObs, Quaternion.identity);
}

//That's for checking the first obstacle with the startList

public bool isConnected = false;

public PipaRotate ThisPipe;

void Start () {
    ThisPipe = transform.parent.GetComponent<PipaRotate>();  
}

void Update () {

}

void OnTriggerStay2D(Collider2D other){
    if (other.tag == "Balok"){

        isConnected = true;

        if (other.transform.parent.GetComponent<PipaRotate>().Check == null && ThisPipe.Check != null){
            ThisPipe.Check.Obstacle.Add(other.transform.parent.gameObject);
            other.transform.parent.GetComponent<PipaRotate>().Check = ThisPipe.Check;
            /*bool isEnter = false; 

            for (int i = 0; i < ThisPipe.Check.Obstacle.Count; i++) {
                if (ThisPipe.Check.Obstacle[i] == other.transform.parent.gameObject){
                    isEnter = true;
                }
            }
            if (isEnter == true){
                ThisPipe.Check.Obstacle.Add(other.transform.parent.gameObject);
                other.transform.parent.GetComponent<PipaRotate>().Check = ThisPipe.Check;
            }*/
        }
    }

    if (other.tag == "Akhir"){
        if (ThisPipe.Check !=null){
            ThisPipe.Check.isConnectAll = true;
        }
    }
}

void OnTriggerExit2D(Collider2D other){
    if (other.tag == "Balok"){

        isConnected = false;

        if (ThisPipe.Check != null){
            int indexPipe = ThisPipe.Check.Obstacle.IndexOf(other.transform.parent.gameObject);
            for (int i = ThisPipe.Check.Obstacle.Count - 1; i == indexPipe; i--) {
                ThisPipe.Check.Obstacle[i].GetComponent<PipaRotate>().Check = null;
                ThisPipe.Check.Obstacle.RemoveAt(i);
            }

        }
    }

//That's for checking the other obstacles with the first obstacle
pipe.png

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.