I don’t even know if my question makes any sense or not but i will try to explain it well.
The game is in 2D.
I’m having a box which is basically an array containing objects (objects are being childs of player and also being deactivated). Those boxes are being instantiated randomly in the scene after a random amount of time.
When player collides with a box, it will make the player activate randomly an object which is a child of the player. What i want to achieve is: when the player collides again with another box and if the currently active object is still there, i want to make the player deactivate the currently active object and activate randomly another one. And so on.
Right now i have come with the solution in the script below, but it 's kinda buggy and i know this isn’t the right way to do it. Sometimes it did the job, sometimes it activated 2 or more objects simultaneously.
So if anyone has better solution, please help. Thank you for reading my question. Here is the script:
public GameObject[] Objects;
GameObject currentObject;
GameObject newObject;
int i;
int j;
private void Update()
{
i = Random.Range(0, Objects.Length);
currentObject = Objects*;*
j = Random.Range(0, Objects.Length);
newObject = Objects[j];
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == “PickupAble”)
{
if(currentObject.activeInHierarchy==false)
{
currentObject.SetActive(true);
Destroy(collision.gameObject);
}
}
if(collision.gameObject.tag==“PickupAble”)
{
if(currentObject.activeInHierarchy==true)
{
currentObject.SetActive(false);
newObject.SetActive(true);
Destroy(collision.gameObject);
}
}
}