Well, we essentially need to do 2 things. First we need the objects. Then we need a way to access them from the C# script, and call the SetActive
method.
- At the main menu, click on GameObject -> Create empty.
- Name the game object something appropriate. Like CubesThatWillAppear.
- At the main menu, click on GameObject -> Create Other -> Cube.
- In your asset hierarchy view, drag the cube into the CubesThatWillAppear GameObject.
- Go to your C# script that is linked to your player object.
- Add a public property to reference the CubesThatWillAppear.
Like so:
public class PlayerController : MonoBehaviour {
public GameObject cubes; // <--- We will reference this.
// Other code
}
Now click on the player object and drag the CubesThatWillAppear into the Cubes property slot that you see. Go back to the C# script and call this code for the objects to appear when you want them to, I can’t tell you in what method to place this, that depends on where you want them to appear.
cubes.SetActive(true);
When you want the cubes to disappear call this code:
cubes.SetActive(false);
1
solved Activating game object under conditions in c# [closed]