0

Recently, I have been making a FPS game in Unity. I need to be able to access certain guns from my prefabs folder. Here is the code:

public class SwitchGuns : MonoBehaviour
{
    public GameObject[] guns;// Holds all the gun prefabs to spawn when switching weapons
    public GameObject gunParent;// The parent object of the gun

    // Use this for initialization
    void Start ()
    {
        guns = Resources.LoadAll("Prefabs/Guns/Guns");// Should load all guns into "guns" array
    }

    // Update is called once per frame
    void Update ()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))// Switches gun to the first weapon in the "guns" array
        {
            spawnGun(0);
        }
    }

    // Spawns a gun from the "guns" array
    void spawnGun(int index)
    {
        GameObject gun = Instantiate(guns[index], gunParent.transform.position, gunParent.transform.rotation) as GameObject;// Creates a bullet at position of gun
    }
}

Also if anyone wants to see the path, here is a picture.

Stekon
  • 1
  • 1
  • What is the issue you're having? Please give a detailed description of what you expect to happen and what is actually happening instead. Also checkout [How do I ask a good question](https://stackoverflow.com/help/how-to-ask) – amura.cxg Feb 07 '18 at 16:24
  • Your Prefab folder should be placed in a folder named **Resources** – Programmer Feb 07 '18 at 16:35

0 Answers0