Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instantiate a single object

Discussion in 'Editor & General Support' started by tiago.23, Mar 2, 2010.

  1. tiago.23

    tiago.23

    Joined:
    Jan 19, 2010
    Posts:
    13
    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. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    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)?
     
  3. tiago.23

    tiago.23

    Joined:
    Jan 19, 2010
    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. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    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 (csharp):
    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? :)
     
  5. tiago.23

    tiago.23

    Joined:
    Jan 19, 2010
    Posts:
    13
    Hi, my code :)

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

    Tiago.
     
  6. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    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:

    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:

    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!
     
  7. tiago.23

    tiago.23

    Joined:
    Jan 19, 2010
    Posts:
    13
    Thank you, HiggyB! You´re very helpful.

    p.s.: ...and i´ll back for the docs.
     
  8. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    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!