Change a boolean within an individual prefab (C#)

Okay, imagine this:
There’s a prefab, and it’s a cube. There’s a bunch of these cubes on scene. The prefabs have a script attached to them with a boolean - the boolean is false. I have written a poetic code that make the boolean true if you click at the cube. The problem is: the boolean changes for all prefab cubes, and not just the one I clicked.

How can I make an “individual” boolean for these prefabs? It has to be possible. I know that many makes their enemies into prefabs, and their health changes individually, so a prefab is therefore capable of individuality. Or am I wrong? Thanks in advance for reading :slight_smile:

If the cubes are in the screen, they are not a prefab. The prefab is the original instance that you create your objects in scene from.

In future, you will probably get better answers if you post code. Just make sure to use Code Tags.

I’m guessing your Boolean is defined as a static, so every instance of your class shares the same value.

Is your boolean marked static? If not, post your clicking logic because it must be firing for all cubes.

It’s not static, and I’ve tried booleans, ints, both private and public. Right now it looks like this (I changed the boolean previously mentioned to an int, just to see if anything changed):

public int CanIMove = 0;

This is how I click:

 private void Update()
        {

            // When you click the box it will be allowed to move.
            if(Input.GetMouseButtonDown (0)){
                CanIMove=1;
            }

            // If you click control, the box will not be able to move.
            if(Input.GetKeyUp(KeyCode.LeftControl))
            {
                CanIMove = 0;
            }

            // If you are allowed to move, the move code below is activated.
            if(CanIMove == 1){
// Bunch of move code.
}

@GroZZleR was correct then. You do not check which box the mouse is clicking on, only that the mouse has been clicked.

Check out the OnMouseDown() message.

1 Like