How to have GameObjects only show in designated camera?

Hi all,

I’m creating a 4 player game where the screen is split into 4 fields. Each player has their own camera that follows them around. Since the game takes place out in space, there are stars and planets in the background to give the illusion of movement and depth. Since the 4 players are independent of each other, each player has their own set of stars and planets that move according to their movement.

The problem I’m running into is when they fly next to each other, the stars and planets for each player is visible in the other player’s camera, which looks really silly.

I’ve read about culling masks, but am not sure how to use them appropriately.

So, the short of it is, how do I:

A) Assign each set of stars/planets to a unique layer
B) Have each player’s camera see everything but the other player’s star/planet layer

I would assume that it would be something like this:

//This is just pseudocode.  All the detailed work of cameras following the players
//stars moving, etc. is ignored here.

GameObject[] player = new GameObject[4];
int numStars = 100;
for (int p = 0; p < 4; p++)
{
    player[p] = new GameObject();
    Camera playerCam = player[p].AddComponent<Camera>();
    playerCam.cullingMask = "MainLayer" && ("Player" + p);

    for (int s = 0; s < numStars; s++) 
    {
        Star newStar = player[p].AddComponent<Star>();
        newStar.setCullLayer = "Player" + p;              //PSEUDO!
    }
}

Again, that’s just all pseudocode, so I know it doesn’t actually work. I don’t know how to assign layers to the objects, or the cameras.
Also, all of the cameras, players, stars, etc. are created with scripts, so almost none of this can be done in the Inspector since they don’t exist there until the game starts.

Thanks for the help!

Alright, I figured it out. The link Dinosaurs provited to Unity’s documentation on layers helped push me in the right direction:

So, since I have (up to) 4 players, I went into the inspector and found the layer menu (which I didn’t previously know about). There’s a Layers drop down menu, and I clicked on “Edit Layers”.
0 - 7 are already spoken for, so I edited layers 8 - 11 to look like this:

8:  Player1
9:  Player2
10: Player3
11: Player4

The names ended up not being important.

From there, I set it up so that all the GameObjects that contained the stars, planets for the players received this command:

GameObject star;
star.layer = 8 + playerNum;   //Where players are number 0 - 3

Then, for each camera, I gave them this code:

Camera playerCamera;
playerCamera.cullingMask = (1 << 0 | 1 << (8 + playerNum));  //Again, playerNum is 0 - 3

The cullingMask is bitwise operation. This sets bit 0 (which is the layer “Default”) and bit 8, 9, 10, or 11, depending on the player.

Then it worked like a charm.

My biggest hiccup was really only the difference between how you set the layer and how you set the cullingMask.
cullingMask is bitwise operation, whereas layer is simply the number for the layer.