Prefab does not activate when colliding with an object

I am doing a project and there is a problem that I am facing.

I have two gameObjects with 2D colliders (coming from prefab) that moves from right to left. When they touch each other, they are deactivated.

I also have an empty game object in which I add a script Respawnerthat randomly generates obstacles.

The problem is that when they touch each other once, they will never be reactivated again.

Respawner Empty GameObject:

enter image description here

The border:

enter image description here

Collections: enter image description here Respawn Script:

public class Respawn : MonoBehaviour {
    [SerializeField]
    private GameObject[] obstacles;
    private List<GameObject> listname = new List<GameObject>();

    void Awake(){
        InitilizeObstacle();
    }

    void Start() {
        StartCoroutine(RandomObstacleSpawn());
    }

    void InitilizeObstacle(){
        int index = 0;
        for (int i=0; i<obstacles.Length * 3 ; i++) {
            GameObject obj  = Instantiate(obstacles[index],new Vector3(transform.position.x,transform.position.y,-2f),Quaternion.identity) as GameObject;
            listname.Add(obj);

            listname[i].SetActive(false);
            index++;

            if(index==obstacles.Length){
                index =0;
            }
        }
    }

    void shuffle(){
        for (int i=0; i<listname.Count; i++) {
            GameObject temp = listname [i];
            int random = Random.Range (i, listname.Count);
            listname [i] = listname [random];
            listname [random] = temp;
        }
    }

    IEnumerator RandomObstacleSpawn(){
        yield return new WaitForSeconds(Random.Range(1.5f,2.5f));

        int index = Random.Range (0, listname.Count);
        while (true) {
            if(!listname[index].activeInHierarchy){
                listname[index].SetActive(true);
                listname[index].transform.position = new Vector3(transform.position.x,transform.position.y,-2f);

                break;
            } else {
                index = Random.Range(0,listname.Count);
            }

            StartCoroutine(RandomObstacleSpawn());
        }
    }
}

Script attach to assembly to move:

public class ObstacleMove : MonoBehaviour {
    private float speed = -1.25f;

    void Start() { }

    void Update() {
        Vector3 pos = transform.position;
        pos.x += speed * Time.deltaTime;
        transform.position = pos;   
    }
}

Scripts are attached to the assembly for the touch frame:

public class BorderTouch : MonoBehaviour {
    void OnTriggerEnter2D(Collider2D target){
            if(target.tag=="Border"){
            gameObject.SetActive(false);
        }
    }
}
+4
2

, :

, . a script :

using UnityEngine;
using System.Collections;

public class borderListener : MonoBehaviour {
        public Respawn rS;

        void OnTriggerEnter2D(Collider2D target){
                rS.spawnIt ();

        }

}

Unity Respawn Border script. !

Respawn script StartCoroutine(RandomObstacleSpawn()); IEnumerator RandomObstacleSpawn(). ( script) Respawn script:

public void spawnIt(){
                StartCoroutine(RandomObstacleSpawn());
        }

:

, :

    while (true) { //A
                if(!listname[index].activeInHierarchy){
                       //B
                    listname[index].SetActive(true);
                    listname[index].transform.position = new Vector3(transform.position.x,transform.position.y,-2f);

                    break; //C
                } else {
                    index = Random.Range(0,listname.Count);
                }

                StartCoroutine(RandomObstacleSpawn()); //D
            }

lil noob, . : while(true) ? ? (: . )

: while (A)

if (B)

break; (C)

StartCoroutine (D) > , .

StartCoroutine(RandomObstacleSpawn()); break;, , , Unity. , ? .

, :

IEnumerator RandomObstacleSpawn(){

            yield return new WaitForSeconds(Random.Range(3.5f,4.5f));
            int index = Random.Range (0, listname.Count);


            if(!listname[index].activeInHierarchy){
                listname[index].SetActive(true);
                listname[index].transform.position = new Vector3(transform.position.x,transform.position.y,-2f);

            }else{

                index = Random.Range(0,listname.Count);

            }

             StartCoroutine(RandomObstacleSpawn());
        }

EDIT: () : R, (TRUE)? ... break; StartCoroutine(RandomObstacleSpawn()); .

+2

, , , gameObject.SetActive(false); gameObject.

, , , .

, , gameObject, Renderer.

gameObject.GetComponent<SpriteRenderer>().enable = false;

true.

, SetActive(false) gameObject Unity.

0

Source: https://habr.com/ru/post/1619687/


All Articles