I have my code, for my weapon:

using UnityEngine;
using System.Collections;

public class Arma : MonoBehaviour {

public static int municao;
GameObject bullet;
GUIText texto;
// Use this for initialization
void Start ()
{

//bullet = GameObject.Find("Bullet");
municao = 10;
texto = GameObject.Find ("qtdMunicao").GetComponent<GUIText>() as GUIText;
}

// Update is called once per frame
void Update () {

texto.text = municao.ToString ();

if(Input.GetKeyDown("i"))
{
if(municao > 0)
{
Instantiate(bullet, transform.position, transform.rotation);
municao--;
}
}
}
}




How can i load the prefab to my variable to use in my instantiate?

You can use the Load method from the Resources API:

// Instantiates a prefab named "enemy" located in any Resources
// folder in your project's Assets folder.
void Start()
{
}

• So, if i have a prefab in a folder called "Prebas" named Bullet, i can do like this ? bullet = Resources.Load("Bullet", typeof(GameObject)); ?? – Enzo Tiezzi Jun 23 '14 at 19:18
• Yes, but everything has to be inside a "Resources" folder. So you should have a path "Resources/Prefabs/Bullet.prefab" and then you could call Resources.Load("Prefabs/Bullet", ...)`. That should do the trick ;) – glampert Jun 23 '14 at 19:21
• So i need to make a folder called Resources ? – Enzo Tiezzi Jun 23 '14 at 19:24
• Yep, the "Resources" folder is mandatory. The "Prefabs" folder I used in the comment above is optional. – glampert Jun 23 '14 at 19:25
• ok, i have a folder called Scripts, and another called Prefabs, so i will put both inside this "Resources" folder, and it should work, right ? – Enzo Tiezzi Jun 23 '14 at 19:26

You can drag-and-drop objects from the Project view just as you would in the main scene, linking them to variables in the Inspector.

That is, click-and-drag the prefab asset in the Project view, and then drop it onto the variable in the Inspector.

• in c# script, the variable does not appear, that's why i'm trying this new way, thank you – Enzo Tiezzi Jun 23 '14 at 22:11
• If it doesn't appear, then you didn't write the code properly, because I assure you I do that all the time. Looking at the code you posted, the issue is that you need to declare the variables as 'public'. In your other question you claimed to already know about the code I posted, but clearly you do not. – jhocking Jun 23 '14 at 23:58