[Solved] Activating game object under conditions in c# [closed]


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.

  1. At the main menu, click on GameObject -> Create empty.
  2. Name the game object something appropriate. Like CubesThatWillAppear.
  3. At the main menu, click on GameObject -> Create Other -> Cube.
  4. In your asset hierarchy view, drag the cube into the CubesThatWillAppear GameObject.
  5. Go to your C# script that is linked to your player object.
  6. 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]