• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 10, 2019 at 05:22 PM by beverbende for the following reason:

was my own mistake in a stupid code error

avatar image
0
Question by beverbende · Jul 10, 2019 at 02:58 PM · prefabprefab-instanceprefab changing at runtime

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Jul 10, 2019 at 04:08 PM 0
Share

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.

1 Reply

  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

141 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Prefabs instantiated at runtime disappear right after 0 Answers

Prefabs spawn with wrong values 0 Answers

Prefabs won't change? 1 Answer

How to change a your player look in once scene and then load it in another 2 Answers

changing instantiated prefabs 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges