1
\$\begingroup\$

I'm working on a Unity project, and what I'm doing is creating a new scene. In my new scene, I want add some prefabs that there aren't in the project folders, but I can see them in another scene.

So in my new scene I have only few prefabs, and I don't know how to add the prefabs that are present in the other scenes. Is there a way to do it?

\$\endgroup\$
3
\$\begingroup\$

It seems that you mix up GameObjects and Prefabs.

When you say "Prefabs in another scenes" I think you are talking about Game Objects.

Game Objects are objects, prefabs or not, who are in a scene. Prefabs are Game Objects that can be "saved" in your game folder. You can find more about Prefabs here.

That said, and if I understand your question correctly, you want to instantiate Prefabs in your scene.

First of all you need to make the GameObject you want to instantiate a Prefab. Go to the scene where the GameObject you want is, and simply drag and drop it in the Unity Project View (where your game folder is). It should now be blue in the scene hierarchy, and visible as a prefab in the folder you dropped it in.

Now go back to the scene where you want to instantiate it. You have 2 options. One via the Editor and one programmatically (in fact you have another option using the Resources.Load but I won't explain it here).

1. Via the editor

To put a prefab in your scene, just click on it in the game folder (in the Project View) and drag it to the Hierarchy view. That's it. Your prefab is now a Game Object of your scene.

2. Via script

In a script of yours (a Monobehaviour) you can declare a prefab entry such as:

public Transform my_prefab;

Now, if you link your script to a GameObject you will see an empty entry called my_prefab. Just drag your Prefab to that entry to fill it with the Prefab you want to instantiate. Later in your code you can instantiate it, in the Start method of your script for example, using:

private void Start() 
{
    if ( my_prefab != null ) // check that the prefab is set.
    {
       Instantiate(my_prefab);
    }
}

Now, at Start, the prefab you have chosen will be instantiated in your scene.

I hope it helps.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Glad it helped. You will surely use the second one when you will have to instantiate stuff programmatically (when you will spawn rockets each time the player press the fire button). So remember that's possible that way :) \$\endgroup\$ – lvictorino Sep 26 '14 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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