Search Unity

Instantiation of a prefab (solved)

Discussion in 'Scripting' started by tet2brick, Jul 15, 2009.

  1. tet2brick

    tet2brick

    Joined:
    Jun 24, 2009
    Posts:
    74
    Can I instantiate a prefab only by script?

    I have a prefab in my project list (so not "active", not in the hierarchy) and I want to instantiate it by script. So not by adding it to a variable in the inspector, but find the prefab by its name or tag or layer and instantiate it.

    Is it possible? :)

    Thanks a lot
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,135
    Why not? Just drag'n'drop.

    --Eric
     
  3. tet2brick

    tet2brick

    Joined:
    Jun 24, 2009
    Posts:
    74
    Because it's an independant script I call in another script. It is not linked to any gameobject :)

    Sorry I was maybe not clear ^^
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,135
    Still not clear I'm afraid...all prefabs are gameobjects. All you have to do is drag the prefab onto the slot in the script. Whether it's in the hierarchy or not is irrelevant...in fact, I'm not sure under what circumstances you'd want to instantiate an object from the hierarchy.

    --Eric
     
  5. tet2brick

    tet2brick

    Joined:
    Jun 24, 2009
    Posts:
    74
    I don't know how to explain exactly.

    There is no slot because the script is not a component of an object. it's just a js file alone, this js file is called by another script and in this independant js file, I need to access a prefab.

    Example:

    this js file is linked to my character

    Code (csharp):
    1. var indeJS : IndependantJS;
    2. if(Input.GetButtonDown("Fire1"))
    3. {
    4. indeJS.dosomething();
    5. }
    6.  
    and this is the independant js file:

    Code (csharp):
    1. var target : gameobject;
    2.  
    3. static function dosomething(){
    4.  
    5. Instantiate(target, someVector3Data, someRotationData);
    6.  
    7. }
    So if the script was a component of a gameobject I can drag and drop the gameobject, but not if the script is independant

    Is it more clear? :)

    Now maybe it's not a good idea to do like that, but I just wanted to know if it was possible

    I can do it another way :)
     
  6. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,612
    You can do that if your prefab is located in a folder called Resources

    See:
    http://unity3d.com/support/documentation/ScriptReference/Resources.html

    Note that any assets in folders with that name will always be included in the game no matter if they are used or not, so use with care!

    Rune
     
  7. tet2brick

    tet2brick

    Joined:
    Jun 24, 2009
    Posts:
    74
    Ho, many thanks :)

    Not exactly the behaviour I expect for now, so I will use another way, but it can be usefull for other projects ;)

    Thanks a lot ;)[/code]
     
  8. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,612
    Well, to clarify, the asset will be part of the game data and thus make the file size of the game bigger. I did not mean that the asset will be visible in your scenes if you don't instantiate it.

    Rune
     
  9. BS-er

    BS-er

    Joined:
    May 24, 2009
    Posts:
    115
    Awesome :). Just as I was struggling with this same issue, the pertinent question is asked and then answered. Thanks :).
     
  10. tet2brick

    tet2brick

    Joined:
    Jun 24, 2009
    Posts:
    74
    Yup, I understood that ;)

    Thanks again ;)