Unity Community


Results 1 to 8 of 8

  1. Posts
    13

    Instantiate a single object

    People,

    I want to instantiate a single object. I'm using the Instantiate function, but it generates a series of clones of my original object.

    Someone knows how i could use this function (or similar) to generate a single clone of my original object?

    Thank you!

    Tiago

  2. Unity Product Evangelist
    Location
    San Francisco CA USA
    Posts
    6,186

    Re: Instantiate a single object

    Quote Originally Posted by tiago.23
    I want to instantiate a single object. I'm using the Instantiate function, but it generates a series of clones of my original object.

    Someone knows how i could use this function (or similar) to generate a single clone of my original object?
    We need more information. Each time you call Instantiate and you give it a prefab reference it only creates on instance of that prefab, this is true in the product today so if you're seeing something different then my guess is that either (a) you're actually calling it repeatedly and just don't know it, or (b) your prefab is not just a single object but many and you don't realize that.

    Can you show us your code? Can you confirm that the prefab you're instancing is in fact only one object (manually drag it into your scene, do you only get the one object you expect)?
    Tom Higgins - Product Evangelist at Unity Technologies ApS
    unity3d.com | answers | blogs | feedback


  3. Posts
    13
    Hi,

    My Instantiate function is inside of an Update function and called by a GetButtonDown.

    Maybe, the Update function multiplies my Instantiate function call when I press the button of the GetButtonDown function. Makes it sense...?

    I'll try to modify the ways that I call these functions.

    Thank you.

    Tiago.

  4. Unity Product Evangelist
    Location
    San Francisco CA USA
    Posts
    6,186

    Re: Instantiate a single object

    Quote Originally Posted by HiggyB
    Can you show us your code?
    Well?

    GetButtonDown will only fire once, even if the user holds down the key/button in question. Please share your code as that will help us help you. For example, the code shown in the script reference entry for Input.GetButtonDown should only generate one instantiated object per press of the "Fire1" button (left mouse), something I can confirm:

    Code:  
    1. // Instantiates a projectile whenever the user hits the Fire1 Button.
    2. var projectile : GameObject;
    3. function Update () {
    4.     if (Input.GetButtonDown ("Fire1")) {
    5.         clone = Instantiate (projectile, transform.position, transform.rotation);
    6.     }
    7. }

    What's your code look like?
    Tom Higgins - Product Evangelist at Unity Technologies ApS
    unity3d.com | answers | blogs | feedback


  5. Posts
    13
    Hi, my code

    Code:  
    1. var obj: GameObject;
    2.  
    3. function Awake(){
    4.  
    5.     obj = GameObject.Find("Obj1");
    6.  
    7. }
    8.  
    9. function Update () {
    10.  
    11.     if(Input.GetButton("Throw")){
    12.         Instantiate(obj, Vector3(0,3,0), transform.rotation);
    13.     }
    14.  
    15. }

    Thanks for your help.

    Tiago.

  6. Unity Product Evangelist
    Location
    San Francisco CA USA
    Posts
    6,186
    And all becomes clear, this is a situation where a bit more careful reading of the docs might have helped you. You're using Input.GetButton, from the doc page it says:

    Returns true while the virtual button identified by buttonName is held down.

    Think auto fire - this will return true as long as the button is held down.
    So, "think auto fire", and that means unless you are lightning quick you just might get more than one instantiation as it's a repeating call. Compare that with the code you said you were using in your second post, and that I referenced above, Input.GetButtonDown whose doc page says:

    Returns true during the frame the user pressed down the virtual button identified by buttonName.

    It will not return true until the user has released the key and pressed it again.
    Which is much more of a single-fire per key press, even if it's held down.


    So, be sure you read the docs when having questions like this and in your case switch from GetButton to GetButtonDown and you'll be all set!
    Tom Higgins - Product Evangelist at Unity Technologies ApS
    unity3d.com | answers | blogs | feedback


  7. Posts
    13
    Thank you, HiggyB! YouŽre very helpful.

    p.s.: ...and iŽll back for the docs.

  8. Unity Product Evangelist
    Location
    San Francisco CA USA
    Posts
    6,186
    Quote Originally Posted by tiago.23
    Thank you, HiggyB! YouŽre very helpful.
    Some times it takes new/fresh eyes to spot remarkably simple stuff... You're quite welcome, these forums are all about helping each other so I'm glad I could lend a hand. Rock on!
    Tom Higgins - Product Evangelist at Unity Technologies ApS
    unity3d.com | answers | blogs | feedback

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •