Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I want to load the prefab instead of the gameobject that is in the screen, my code is this one:

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--;
        }
    }
}

} `

share|improve this question
    
Find() is used to find objects that already exist in your unity scene. –  Wardy Jun 23 at 15:50
add comment

2 Answers 2

up vote 2 down vote accepted

The equivalent code in C# is

public GameObject object;

referring to your additional edits:

Your code will instantiate whatever object is referenced in the "bullet" variable, so just make sure to stick the object you want in there. In your case, it sounds like what you want is delete the GameObject.Find() line for "bullet" and instead drag-and-drop the prefab asset; in the editor, drag the prefab from the Project view over to the Inspector.

share|improve this answer
    
i know it, the problem is, i need to put something inside this object, a game object, or a prefab, how can i do it ? –  Enzo Tiezzi Jun 23 at 15:38
    
i believe what you want is the Instantiate() method Enzo –  Wardy Jun 23 at 15:48
    
you can also just say object = new GameObject("Some name"); then start adding components to it with object.AddComponent<T>() –  Wardy Jun 23 at 15:49
    
let me explain better, i have a weapon script, that in this weapon a have bullets, i have a GameObject and a prefab for the bullet, then in the weapon i create a GameObject variable called bullet, but if a instantiate this bullet, i will get an exception, because this GameObject bullet is null –  Enzo Tiezzi Jun 23 at 15:50
    
ok updated my answer to fit this –  Wardy Jun 23 at 15:56
show 1 more comment

The "roll it yourself" way ...

public class Gun : MonoBehaviour
{
   public List<GameObject> bullets;

   void Start()
   {
      var bullet = new GameObject();
      bullets.add(bullet);
      something.AddComponent<BulletBehaviour>();
   }
}

The "load something I already configured" way ...

public class MyThing : MonoBehaviour
{
   public Transform bulletPrefab;
   public List<GameObject> bullets;

   void Start()
   {
      bullets.Add(Instantiate(bulletPrefab) as Bullet);
   }
}

EDIT: Based on your change of question (not recommended by the way) I would say that what you should do is declare a new variable of type Transform and in unity (before running) drop your bullet prefab on to that in the inspector.

That gives unity a reference to the asset not some instance of an object. You can then call instantiate on the ref ...

using UnityEngine;
using System.Collections;

public class Arma : MonoBehaviour {

  public static int municao;
  //GameObject bullet;
  Transform bulletPrefab; // set this in the unity editor before runtime
  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(bulletPrefab, transform.position, transform.rotation);
            municao--;
        }
    }
  }
}
share|improve this answer
    
Obviously you wouldn't put this code in Start but in some other method you might call like maybe in update you could do some sort of key press check then call a method with this code in it. –  Wardy Jun 23 at 15:58
    
when a arrive at home, i will remake the question. thank you !! :D –  Enzo Tiezzi Jun 23 at 15:58
    
i remake the question –  Enzo Tiezzi Jun 23 at 18:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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