make a script unique to the gameobject it is attached to

Okay lets try this again: I posted a similiar question to this one before but i see that things got complicated and people probably didnt understand what i wanted to accomplish. Im going to try again but try to make it more clear and simple to understand.

In my project i can spawn several gameobjects and all these objects carry a script. Becuase of this i will have several instances of the same script running in the scene. What i want to figure out is how i can make this script only behave to the gameobject i am accessing.

The variable that i want to be unique for every single gameobject is an int (stateCount) that i use in a switch-statement. This int tracks the state of the gameobject and its behaviour and since all the gameobjects react to this they will follow along even though i only wanted to access 1 gameobject.

Do i need to make a new class for this int? Can i get a ID for each gameobject spawned and that way will make each script unique? Do i need to make a new way on how i handle the changing of this int?

If i didnt make myself clear this time, please let me know in the comments section if you dont understand. Rather that then people ignoring the question since it seems complicated and obscure.

So once i add the script to the gameobject (and not that it already has it when its instantiated) its values and variables are unique only to the gameobject and not all other?

Well, when you instantiate a gameobject, all scripts attatched to it are instantiated aswell, unless their variables are static, they are unique in that when you change them, no other gameobjects with the same script will change aswell.

Unless ofcourse you accidentally change the variable of all instances to this other value. Which is what I think is happening (But I’m not sure because I don’t have all the info).

If all objects have the same script, and each script does exactly the same thing in it’s update loop, then the results will all be the same. The values aren’t shared, they just happen to have the same value because they all did the same.

For instance, in the Update loop, “Input.GetMouseButtonDown(0)” is true for each instance of a script. (As it doesn’t check where the mouse is, it just checks if the button is held down) So each script is gonna say, yes button is clicked, lets raycast. Yes we raycast agains something, lets put our own stateCount to 0 or 1. So each instance sets its own value to the same value individually.

That’s why I asked when PlaceShip is called. It should only be called only on the object of which you want stateCount to change, if you just call PlaceShip in update, then each gameObject with this script attatched will do the exact same thing, as for everyone, the mousebutton is held down, and the ray from the mouse position to the world will hit the exact same object

Okay i fixed my problem now with the help i got. The issue was that i needed to call PlaceShip() somewhere else and not in the ship script. I did a script that focuses on raycasts on the camera that will call the function from the ship script. The problem was that i couldnt access the ship i was currently holding since the way i used raycast was to detect tiles where i could place the ship, not that i was using the raycast to detect the ship.

The thing i did was to create a parent where i assigned the gameobject to when spawned. Through the camera/ray script i managed to access this parent then do a foreach loop to detect the child (the ship) and access through a empty gameobject variable called aquiredship in the camera script. There i could access the ship script and from there it was easy. Whenever i had a ship selected, the aquiredship variable got access to it and whenever i didnt want it selected i just set the variable to null

Thanks @Bunny83 & @troien