Search code examples
arraysunity-game-engineinstantiationdestroygameobject

Unity prefab array instantiate and destroy


I am having a very little problem in my Unity project but can't find a proper help or way to do. I am stuck at point where I have an array of prefab GameObjects and I am trying to instantiate index 1 GameObject and when it destroyed instantiate the next index. Here is how I am doing it. I have two scripts: One to instantiate and other one to destroy it. Scripts 1:

public class GameObjectsArray : MonoBehaviour {

    public static GameObjectsArray Instance { get; set; }
    public GameObject[] Objects;
    public int i=0;

    // Use this for initialization
    void Start()
    {
        InstiatingMethod();
        //Instantiate(Objects[i]);

    }
    public void InstiatingMethod()
    {
        Instantiate(Objects[i]);
    }
}

Scripts 2:

public class CheckDestroy : MonoBehaviour {

    //public GameObject[] Objects;
    //int i;

    // Use this for initialization
    void Start () {

        Debug.Log("executed");

        //Objects = GameObject.FindGameObjectsWithTag("Player");

        //OnMouseDown();

        //Instantiate(Objects[i], transform.position, transform.rotation);

    }

    // Update is called once per frame
    void Update () {

        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                BoxCollider boxCollider = hit.collider as BoxCollider;

                if (boxCollider != null)
                {
                    GameObjectsArray.Instance.i++;
                    GameObjectsArray.Instance.InstiatingMethod();

                    Destroy(boxCollider.gameObject);

                }

            }
        }
    }
}

Solution

  • So I created a very quick project to make a good response:

    Scene Image

    In the scene, we will have an empty game object that will contain our script that I called "GameManager", basically this script will do everything, it's more logic to put your logic in one script.

    public GameObject[] GameObjects;
    
    private int _targetIndex = -1;
    private RaycastHit _hit;
    
    private void Start()
    {
        InstantiateNextGameObject();
    }
    
    public void InstantiateNextGameObject()
    {
        //if the index is pointing at the last game object in the array, init the index to -1
        if (_targetIndex == GameObjects.Length - 1)
            _targetIndex = -1;
    
        Instantiate(GameObjects[++_targetIndex]);
    }
    
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out _hit))
            {
                BoxCollider boxCollider = _hit.collider as BoxCollider;
    
                if (boxCollider != null)
                {
                    InstantiateNextGameObject();
                    Destroy(boxCollider.gameObject);
                }
            }
        }
    }
    
    • we will have an array of gameobjects following with the targetIndex which will start from -1.

    • The function InstantiateNextGameObject() will simply increment targetIndex and then instantiate a gameobject from the array (++targetIndex the first time will be 0, second time 1 etc). We have to check also if the targetIndex reaches the end of the array, put it back to -1.

    • Then basically what you did in the update, when you click on a gameobject, instantiate the next one and destroy the current.

    At the end, you will get something like that:

    https://ayoub-gharbi.org/youba_docs/stackoverflow/stackoverflow01.mp4

    Feel free to ask me if you didn't understand anything:)

    Happy coding!