I am very new to programming and I wanted to know how to make a gameObject turn on/off its visibility and collision at random when I press start. I’m making a maze that has a few walls change every time the player restarts the game. I assume its something to do with true/false statements.
Hello.
Its simple. You just need to use SetActive function
You will need to find all walls that can be disabled and place it in a array.
Version1: You can make a public GameObject array and assign all walls that can be disabled 1 by 1 into the that array from the inspector.
public GameObject[] wallsFordisable;
Version2: Another good solution is to make a new Tag and assign the tag to that walls (for example “WallsForDisable”). Then find all of them via script (so no need to place them 1 by 1 in inspector) to create the array in the moment you do the foreach.
And do something like this:
void Start()
{
//version1:
foreach (GameObejct oneWallForDis in wallsFordisable)
//version2:
foreach (GameObejct oneWallForDis GameObject.FindGameObjectsWithTag("WallsForDisable"))
{
float randomNumber = Random.Range(0,1);
if (randomNumber > 0.5f) oneWallForDis.SetActive(false);
}
}
So when iterating thorugh all walls in that array, it will get a random nuber from 0 to 1, and if its higher than 0.5 it will disable the object (so no collisions/rendering).
Bye!