The example is simplified:
if i click on a certain spot in my scene, a turret should spawn which is a prefab. (this goes well)
i do this with Instaniate(turretprefab, coordinates, quaternion.identity);
The turret is a prefab with a script attached which instantiates the variable (in the start method) power = 0;
when i click on a turret, the power should increase by 1. (this goes well)
however when i create another turret and press on this turret, it has the same value of the first turret that was instantiated.
How can this be fixed? i want both turrets to have seperate variables instead of sharing the same value. meaning clicking on turret 1 a 100 times should give this turret power = 100. and clicking on turret2 twice should give it power = 2.
It might be located in the script that you have the power increases with, without seeing it I couldn’t tell you for sure if that is the issue, but I’ve had trouble with instantiating objects in my projects in the past and avoid it all costs; I usually create the object in the scene, set it to inactive, then when I need to “instantiate” it I just set the object to active and move it’s position to wherever I would need it.
Regardless of what is causing the error, you should consider object pooling rather than instantiating; it usually offers better control over the objects individually and is much better on performance.
Here is an excellent online tutor going over the exact subject, his stuff is usually pretty legit and I’ve learned a ton from Brackey’s, but I haven’t watched this particular video: