- Home /
When i Instantiate new objects what should i do with the original object ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InstantiateObjects : MonoBehaviour
{
public GameObject prefab;
public Terrain terrain;
public float yOffset = 0.5f;
public int objectsToInstantiate;
public bool parent = true;
private float terrainWidth;
private float terrainLength;
private float xTerrainPos;
private float zTerrainPos;
private List<GameObject> prefabs = new List<GameObject>();
void Start()
{
//Get terrain size
terrainWidth = terrain.terrainData.size.x;
terrainLength = terrain.terrainData.size.z;
//Get terrain position
xTerrainPos = terrain.transform.position.x;
zTerrainPos = terrain.transform.position.z;
generateObjectOnTerrain();
}
public void generateObjectOnTerrain()
{
for (int i = 0; i < objectsToInstantiate; i++)
{
//Generate random x,z,y position on the terrain
float randX = UnityEngine.Random.Range(xTerrainPos, xTerrainPos + terrainWidth);
float randZ = UnityEngine.Random.Range(zTerrainPos, zTerrainPos + terrainLength);
float yVal = Terrain.activeTerrain.SampleHeight(new Vector3(randX, 0, randZ));
//Apply Offset if needed
yVal = yVal + yOffset;
//Generate the Prefab on the generated position
GameObject objInstance = Instantiate(prefab, new Vector3(randX, yVal, randZ), Quaternion.identity);
if (parent)
objInstance.transform.parent = this.transform;
prefabs.Add(objInstance);
}
}
public List<GameObject> PrefabsList()
{
return prefabs;
}
}
I have one empty GameObject i renamed it to Teleportation Booths. And also tagged it as: Teleportations. On this gameobject i attached the script.
Then under it as child i have another gameobject renamed it to: Teleportation Booth and tagged it as: Teleportation.
Now when i make a clone of the child it's creating duplicated 20 childs. But the original object i'm using to duplicate is not counting as child.
I mean that in another script when getting all clones the original is not counter. It should be 21 but it's 20: Now the List TeleportBooths contain 20 items. But i want to use also the original one not only the 20.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Teleport : MonoBehaviour {
private List<GameObject> TeleportBooths;
private void Start()
{
InstantiateObjects gos = GetComponent<InstantiateObjects>();
TeleportBooths = new List<GameObject>();
TeleportBooths = gos.PrefabsList();
for(int i = 0; i < TeleportBooths.Count; i++)
{
TeleportBooths[i].AddComponent<TeleportationsCore>();
}
}
}
Answer by Joe-Censored · May 16, 2017 at 05:38 PM
I think you have a misunderstanding of what a prefab is supposed to be. Prefabs are gameobjects that you have in your assets folders, such as under resources, that you instantiate. You don't need one already in the scene first. So what to do with the one already in your scene is to just not have one already in your scene in the first place.
Also, naming your list of instantiated gameobjects as "prefabs" is very confusing, since they aren't prefabs.
So the original gameobject the one i'm cloning should be as prefab in the assets folder(Should i create a folder in the editor in the Assets and call it Prefabs ?) not in the scene and not in hierarchy and then i just drag it from the assets as prefab to the script in the inspector ?
And then i'm just using in the scene the 20 cloned objects.
Did i understand it right ?
As Todd already pointed out, you nailed it exactly. What you name the folder is up to you, but placing them somewhere under a folder named Resources is sometimes desirable as that folder has special significance for built in methods like Resources.Load.
https://docs.unity3d.com/ScriptReference/Resources.Load.html
I think i'm getting it. I did Assets > Create > Prefab Then i dragged the GameObject in the Hierarchy to the new created Prefab in the Assets.
Now i'm using this new Prefab in the inspector in the script for cloning right ?
Yeah correct. The purpose of prefabs are to be used as templates to create instances from. You can essentially take any object in your scene and turn it into a prefab (by dragging it into your assets/project view) for you to quickly create instances of it either dynamically during runtime ( like you would with, say, bullets), or during development while working on your scene.
However, you can still clone objects that are in your scene that are not prefabs at all, which is kind of what you were originally doing.
Yep and you can skip the Assets > Create > Prefab step. $$anonymous$$y typical workflow when I am creating a new prefab is I build it all in the scene, and when I'm satisfied I just drag the gameobject into my assets/resources folder. Then I delete the original in the scene and link up whatever code is going to instantiate the new prefab.
Your answer
![](https://koobas.hobune.stream/wayback/20220612122134im_/https://answers.unity.com/themes/thub/images/avi.jpg)