0
\$\begingroup\$

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?

\$\endgroup\$
2
\$\begingroup\$

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() 
{
    GameObject instance = Instantiate(Resources.Load("enemy", typeof(GameObject)));
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ So, if i have a prefab in a folder called "Prebas" named Bullet, i can do like this ? bullet = Resources.Load("Bullet", typeof(GameObject)); ?? \$\endgroup\$ – Enzo Tiezzi Jun 23 '14 at 19:18
  • \$\begingroup\$ 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 ;) \$\endgroup\$ – glampert Jun 23 '14 at 19:21
  • \$\begingroup\$ So i need to make a folder called Resources ? \$\endgroup\$ – Enzo Tiezzi Jun 23 '14 at 19:24
  • \$\begingroup\$ Yep, the "Resources" folder is mandatory. The "Prefabs" folder I used in the comment above is optional. \$\endgroup\$ – glampert Jun 23 '14 at 19:25
  • \$\begingroup\$ 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 ? \$\endgroup\$ – Enzo Tiezzi Jun 23 '14 at 19:26
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ in c# script, the variable does not appear, that's why i'm trying this new way, thank you \$\endgroup\$ – Enzo Tiezzi Jun 23 '14 at 22:11
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – jhocking Jun 23 '14 at 23:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.