ToggleWeapon: Need advice/tips and how to.

Hi, I have been working on a script to toggle my weapons. This isn’t to initially get the weapon. My goal, is to select a weapon from a menu then it send that weapon to the starting phase which will be my “primHold” and “secHold”. I currently have my buttons setup to where they function like I am aiming for and now am at a point I do not know where to go or what to do.

My weapons are prefabs. I have them set to if I drag the prefab to my primHold slot or secHold slot they line up perfect as well as my primSlot and secSlot. primHold allows my weapon when not in use to sit on the players back, and primSlot sets it to the hands. They are both empty transforms.

I “think” to achieve the swap I need to get the child in primHold object and send that transform to new parent being my primSlot. Not sure if that’s right and if so I am not sure how to achieve that. Being same for secondary too.

Right now I am trying to accomplish switching between the states when the button is pressed. Below is a picture of what I want my start setup to look like when first enter the game. Also a script of my toggle manager. I know there’s a lot needed to be added but I have started this way to learn basic function and such. Any advice on how to accomplish this? eventually my weapons will be spawned by a main menu selection, and that weapon be set to the location. I have another script with some kinda array I am trying to learn as well. I’m pretty new to scripting. I’ll show both scripts and then any guidance on how to implement the two would be amazing. Thanks!

WeaponToggle::

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;

public class WeaponStateHandler : MonoBehaviour{

    PrimaryItem primItem;

    Animator anim;
    Toggle toggle;

    GameObject primSlot;
    GameObject primHold;
    GameObject secSlot;
    GameObject secHold;





    void Awake()
    {
        primSlot = GameObject.FindGameObjectWithTag ("PrimSlot");
        secSlot = GameObject.FindGameObjectWithTag ("SecSlot");
        primHold = GameObject.FindGameObjectWithTag("primHold");
        secHold = GameObject.FindGameObjectWithTag("secHold");

    }
    void Start()
    {
        anim = GetComponent<Animator> ();
        toggle = GetComponent<UnityEngine.UI.Toggle> ();
    }

    void Update()
    {

    }


    public void ToggleAim(Animator anim)
    {
        anim.SetBool ("Aiming", toggle.isOn);
    }

    public void TogglePrimary(Animator anim)
    {   
        primSlot.transform.gameObject.SetActive (true);
        secSlot.transform.gameObject.SetActive (false);
        anim.SetBool ("DrawPrimary", true);
        anim.SetBool ("DrawSecondary", false);
    }

    public void ToggleSecondary(Animator anim)
    {
        secSlot.transform.gameObject.SetActive (true);
        primSlot.transform.gameObject.SetActive (false);
        anim.SetBool ("DrawSecondary", true);
        anim.SetBool ("DrawPrimary", false);
    }

    public void FreeHand(Animator anim)
    {
        secSlot.transform.gameObject.SetActive (false);
        primSlot.transform.gameObject.SetActive (false);
        anim.SetTrigger ("Holster");
        anim.SetBool ("DrawSecondary", false);
        anim.SetBool ("DrawPrimary", false);

    }
}

WeaponArray:: small example cause I don’t know what to do lol.

using UnityEngine;
using System.Collections;


public class PrimaryItem : MonoBehaviour {

    public PrimaryType type; 

    public enum PrimaryType
    {
        M4A1,
        AK47
    };

    void PrimWeapon()
    {
        switch (type)
        {

        case PrimaryType.M4A1:
            break;

        case PrimaryType.AK47:
            break;  
        }
    }
}

Goal: Start of Game:

Goal: After Primary has been toggled on:

So I have messed around some more and done the following:

I set a prefab of the item in both primSlot, secSlot, primHold, secHold. I changed my code to the following below:
By doing that I achieved the results I am wanting. Now, If I was to make several prefabs and set into those slots to maintain the following results, how do I set all false until the weapon ID is called to show that transform into that slot?

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;

public class WeaponStateHandler : MonoBehaviour{

    PrimaryItem primItem;

    Animator anim;
    Toggle toggle;

    GameObject primSlot;
    GameObject primHold;
    GameObject secSlot;
    GameObject secHold;

    bool primDraw;
    bool secDraw;





    void Awake()
    {
        primSlot = GameObject.FindGameObjectWithTag ("PrimSlot");
        secSlot = GameObject.FindGameObjectWithTag ("SecSlot");
        primHold = GameObject.FindGameObjectWithTag("primHold");
        secHold = GameObject.FindGameObjectWithTag("secHold");

    }
    void Start()
    {
        anim = GetComponent<Animator> ();
        toggle = GetComponent<UnityEngine.UI.Toggle> ();
        secSlot.transform.gameObject.SetActive (false);
        primSlot.transform.gameObject.SetActive (false);
    }

    void Update()
    {

    }


    public void ToggleAim(Animator anim)
    {
        anim.SetBool ("Aiming", toggle.isOn);
    }

    public void TogglePrimary(Animator anim)
    {   
        primDraw = true;
        primSlot.transform.gameObject.SetActive (true);
        secSlot.transform.gameObject.SetActive (false);
        anim.SetBool ("DrawPrimary", true);
        anim.SetBool ("DrawSecondary", false);

        if (primDraw == true)
        {
            primHold.transform.gameObject.SetActive (false);
            secHold.transform.gameObject.SetActive (true);
        }
    }

    public void ToggleSecondary(Animator anim)
    {
        secDraw = true;
        secSlot.transform.gameObject.SetActive (true);
        primSlot.transform.gameObject.SetActive (false);
        anim.SetBool ("DrawSecondary", true);
        anim.SetBool ("DrawPrimary", false);

        if (secDraw == true) 
        {
            secHold.transform.gameObject.SetActive (false);
            primHold.transform.gameObject.SetActive (true);
        }
    }

    public void FreeHand(Animator anim)
    {
        secSlot.transform.gameObject.SetActive (false);
        primSlot.transform.gameObject.SetActive (false);
        secHold.transform.gameObject.SetActive (true);
        primHold.transform.gameObject.SetActive (true);
        anim.SetBool ("DrawSecondary", false);
        anim.SetBool ("DrawPrimary", false);

    }
}

If I’m understanding you correctly, you could create a list of all possible weapons:

public List<GameObject> primaryWeapons; // drag all of your weapons into here in the inspector

Then create a helper function:

public void EnableWeapon(string name)
{
    foreach(GameObject weapon in primaryWeapons)
    {
        if(weapon.name == name)
        {
            weapon.SetActive(true);
        }
        else
        {
            weapon.SetActive(false);
        }
    }
}

Then your Start becomes:

void Start()
{
  anim = GetComponent<Animator> ();
  toggle = GetComponent<UnityEngine.UI.Toggle> ();

  // these probably aren't needed any more?
  secSlot.transform.gameObject.SetActive (false);
  primSlot.transform.gameObject.SetActive (false);

  EnableWeapon(""); // "" will make sure no weapon is enabled, they all get disabled
}

And finally, whenever you’re ready to switch weapons, just call EnableWeapon(“name of the weapon”) and make sure that name matches the name of the object in your hierarchy.

Hopefully that helps.