I have a problem to instantiate prefab…
I have done a prefab (a simple cube) in asset.
I can drag in scene this prefab and ok i have an instance linked to original prefab in asset.
I want to do same things in script…
I have this script to create many instance of my prefab:
//This script is attached to a simple gameobjet of the scene
var cube : Transform; //set in inspector with my prefab
function Start () {
for (var y = 0; y < 5; y++) {
for (var x = 0; x < 5; x++) {
cube.Instantiate(cube, Vector3 (x, y, 0), Quaternion.identity); //Generate a CLONE !! not an Instance like drag&drop do
}
}
}
But this code generate CLONE of prefab not linked to, instead of a linked instance like manual drag&drop.
An idea, a solution ?
The whole point of Instantiate is that it generates clones. They aren’t supposed to be linked. The only difference is that you can’t do “Apply changes to prefab” with the clones, which isn’t something you’re likely to want to do during run-time anyway.
In fact i want to generate many (for exemple: 1000) dynamic copy of a gameobjetc witch use the same Mesh the Same material… i don’t want Unity create 1000 Mesh, 1000 material…
And with my code i can see 1000 material generated (in material selection list in inspector) and probably 1000 mesh too… too much for iphone support
Dragging a prefab into the scene manually doesn’t actually do anything different than Instantiate does. The only difference is that the editor keeps a link between the prefab in your scene and the prefab in your project, so you can apply or revert changes. But this is only relevant in the editor…both “proper” prefabs and Instantiate clones behave identically in a build and use the same resources.
If you’re getting different materials generated, that means you’re altering the materials of the objects you’re instantiating and thus creating new ones; this in itself isn’t related to using Instantiate. If you don’t want this to happen, use sharedMaterial instead. Similarly, the same mesh will be used for everything unless specified otherwise. This is why “mesh” and “sharedMesh” exist, so you can either create a unique mesh for specific objects, or change the mesh shared by a number of objects.
but this gets me only the prefab already in scene, not the one in the assets.
If I remove from the scene the manually dragged instance, the code breaks, and returns me a Null reference exception on the line trying to Find(“prefabname”).
I tried also renaming differently the Project prefab, and “looking” for that one, with Find, but no luck!
I really don’t want to waste a draw call or even a gameobject memory allocation for a prefab that I could instantiate directly from the Assets folder.
Perhaps I got it all wrong, and this simply cannot be done. And I MUST instantiate before the prefab in scene…
If is possible, how do I write the same operation with an Assets prefab ?
I’m using unity iphone though, but I think the mechanism should be the same…
I ask this, because after 4 months development, I discovered that I don’t like to have gameobjects with other Transform or GameObjects assigned through a panel. This is hassle when you have many levels/scenes, and every time you have to link back dozens of objects in dozens of scripts vars. Much better do a GameObject.Find at Start/Awake and getting the name, unfortunately this is working for me only with prefabs already present in scene.
From Unity iPhone documentation, I read:
var wreck : GameObject;
// As an example, we turn the game object into a wreck after 3 seconds automatically
function Start () {
yield WaitForSeconds(3);
KillSelf();
}
// Calls the fire method when holding down ctrl or mouse
function KillSelf () {
// Instantiate the wreck game object at the same position we are at
var wreckClone = Instantiate(wreck, transform.position, transform.rotation);
// Sometimes we need to carry over some variables from this object
// to the wreck
wreckClone.GetComponent(MyScript).someVariable = GetComponent(MyScript).someVariable;
// Kill ourselves
Destroy(gameObject);
}
But
var wreck : GameObject;
Implies that I choose from a crazy pulldown with 1820 objects the proper one every time! (for every scene o for every prefab I put in a scene which carries a script which references to the Wreck object)
What is the way to “Get” or “Find” the objects in Assets ??