Conflicting functions

So I have a script that makes the gun sway from side to side, a script that makes the gun move to a new position when the right mouse button is clicked (Iron sights) and I’m also going to have a recoil script.

Problem is that every one of these functions are trying to change the position of the gun. Do you have any suggestions on how to solve this?

Simple way to me would be to create a static variable on one script (like an int) and have that change for each state. Then each script would be watching the same variable, and only affect the movement if it’s the active one. Like:

if(StaticScript.gunState == 1)
// code to move left/right
if(StaticScript.gunState == 2)

etc…

hope this helps

You need to manage your gun state. Create an enum with different states in it, then you can select which script to enable/disable whenever you change states. Try something like this:

public enum State {
  Walking,
  IronSight,
  Recoil
};

public State state = State.Walking;

void ChangeState(State s)
{
  // Only change state if it's different than the current state
  if (s != state)
  {
    state = s;

    // Disable code for all three states by default

    switch (state)
    {
      case State.Walking:
        // Enable code to sway left/right
        break;

      case State.IronSight:
        // Enable iron sight code
        break;

      case State.Recoil:
        // Enable recoil code
        break;
    }
  }
}