Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to attach different components to the same public variable

Discussion in 'General Discussion' started by VintageClaymore, Jul 9, 2023.

  1. VintageClaymore

    VintageClaymore

    Joined:
    Jun 6, 2023
    Posts:
    1
    so basically, I'm working on my first game, an FPS, and I'm trying to get guns to work, and to be as modular and compact in code as possible. I've got a "weapon manager" script that handles all the basic functions that each gun has, like shooting, reloading, etc, and is the script that takes inputs from the player. I have individual scripts for each weapon that basically has all the stats for that weapon (damage, ROF, ammo capacity, etc) and functions for specific actions (such as an under-barrel grenade launcher). The manager script pulls the stats from the weapon scripts so the current gun has the correct attributes.

    How do I attach different components to the same public variable depending on which gun is selected?
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,317
    While you can attach and remove components from objects at runtime using AddComponent, you shouldn't need to do that.

    Basically, there's no need to make a "manager" because guns do not interact with each other. Instead you can make a "Weapon" script, attach it to player or enemeis, reduce gun "scripts" to a pure scriptableobject data block and make the "Weapon" script act in accordance to currently selected datablock data.

    For changing data you could make a public property that would simply forward relevant data.
     
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,503
    A variable can only refer to one thing at a time. So, you'd need some code somewhere which determines which component the variable should refer to, and updates it accordingly as needed.

    Can you tell us more about how weapons work in your game? Stuff that would be useful to know:
    - How many are they?
    - Are they designed in advance, or randomly generated, or a bit of both?
    - Do they fit into specific categories (machine guns, shotguns, rocket launchers, etc.) or can any gun have any stat / feature attached?
    - How many guns is the player likely to have at once?
    - Do enemies use the same guns as players?

    I ask this stuff as I'm not sure what problems you're trying to solve in your design, and it seems to be a bit over-complicated for the weapon system in your first game.

    I agree that neg's suggestions would make things nice and scalable, but given that it's your first game suggest starting with something nice and simple:
    1. Put the stats and all other configuration straight in a Weapon script.
    2. Make one prefab per weapon with that weapon's config put straight into the component.
    3. For the player weapon switching, instantiate one copy of each weapon they have, set the selected one's GameObject to active and keep a reference to it in the player script, deactivate all of the others.

    Yes, that approach has limitations, but you can make it work and cover the basics easily, and start using it. When you run into the limitations you'll know which of them actually matter to your game, rather than trying to guess in advance while you're still trying to figure out how stuff works.

    - - -

    P.S. This is a scripting question, it should go in the Scripting section.