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\$
1

2 Answers 2

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)));
}
\$\endgroup\$
10
  • \$\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\$ Commented Jun 23, 2014 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\$ Commented Jun 23, 2014 at 19:21
  • \$\begingroup\$ So i need to make a folder called Resources ? \$\endgroup\$ Commented Jun 23, 2014 at 19:24
  • \$\begingroup\$ Yep, the "Resources" folder is mandatory. The "Prefabs" folder I used in the comment above is optional. \$\endgroup\$ Commented Jun 23, 2014 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\$ Commented Jun 23, 2014 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.

\$\endgroup\$
2
  • \$\begingroup\$ in c# script, the variable does not appear, that's why i'm trying this new way, thank you \$\endgroup\$ Commented Jun 23, 2014 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\$ Commented Jun 23, 2014 at 23:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.