- Home /
was my own mistake in a stupid code error
Changing the variable 'power' of ONE turret instead of ALL turrets
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.
Please, edit your question and provide your current script.
Your issue could be caused by a variable declared static
. In this case, you have to remove the static
keyword.
Answer by I_Am_Err00r · Jul 10, 2019 at 04:02 PM
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: https://www.youtube.com/watch?v=tdSmKaJvCoA
Follow this Question
Related Questions
How to hide a character prefab 1 Answer
How can I switch between prefab variants in runtime. 1 Answer
Prefabs instantiated at runtime disappear right after 0 Answers
Prefabs spawn with wrong values 0 Answers
Prefabs won't change? 1 Answer