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.