Logic behind multiple copies of a prefab?
I'm new to unity and I'm working on a very basic game until I learn Unity however, I don't know how I should logically restructure my scripts or how to Google this issue.
I have a shop that spawns an instance of a gun prefab; this works fine except when I shoot/reload/anything on the players gun it mirrors it to the gun in the shop. How should I go about fixing it? I just need the logic behind my mistakes and how an ideal basic setup would work.
Answer by wyatts · May 10, 2019 at 10:36 PM
Hard to tell exactly what your problem is without seeing the scripts. Generally you want all functions (like shooting/reloading/anything) to be private to that object, so that you're not accidentally triggering things across multiple objects. Look up global variables and why you should use them sparingly. Good luck
Here's what I have so far, it's an interactable that takes its purchasable item and instantiates it onto the prefab. However when my player object shoots/reloads so does the instantiated prefab. If you could just point me in the right direction I can figure out the details. https://pastebin.com/BimBjArV
I get the issue, but I still can't tell specifics from this script alone. I'd guess it most likely has to do with Player$$anonymous$$isc? It's not specifically this script that is the problem, it's how your scripts are referencing each other. Again I strongly recommend to brush up on the fundamentals of global & static variables & classes, because that's the root of your issue.
Answer by Shreka · May 12, 2019 at 07:37 AM
Are u using any scriptable objects which could trigger the following error...... Maybe u might have ur weapons on the walls actively checking for inputs.... Remember to disable your weapons script for all except for players.... Just disable the gun script on them and enable when it equipe and again disable when equiped......
Yeah this was my issue, I forgot to make the gun check if it was even owned by the player lol. Thanks for that bit of oversight!