- Home /
Preventing first instance of object from starting on screen
I'm making my character throw projectiles at the press of a button. So I created that object in the tree on the left, and when you press the button, I use Instantiante at the character's location.
However this one first instance I created starts in the middle of the screen (where I created it in the Scene editing) and does its launching code before anything happens. Not only that, but when it gets deleted as per normal projectile protocol, the whole class stops working altogether and I can't create any more.
How am I supposed to handle this exactly? I need to 1. Prevent the object from automatically spawning just because it's on my Scene, and just serve as a prefab of which I can create clones which do their thing 2. Find out why this first instance has to exist otherwise clones cannot be created anymore
Answer by Cornelis-de-Jager · Jul 05, 2017 at 12:11 AM
Why do you want to create clones of a prefab already In the scene? Just reference the prefab from the asset folder. That why you can create clones of an object that is not in the scene. I believe that will solve both your issues. Also add code so we can see how you delete the objects.
I realize now I was using the prefab term wrong. I did not know about the actual Prefab feature in Unity. I will start with documenting myself about that, thanks for your help.
Its easy, let me explain how it works.
Create the game object you want in the scene.
Drag it to your asset folder (BOO$$anonymous$$! There you have a prefab)
Delete the object in the scene (it will not be deleted from asset folder)
so if you want to reference them in a script then do this:
/* Create a variable for the prefab (has to be public or serialized) */
public GameObject myPrefab; // This is where you drop and drag the object from the asset folder into the inspector
/* Spawn the object */
// Spawn the object and leave it alone
Instantiate (myPrefab, position, rotation, parent);
// Spawn the object and manipulate it via code
GameObject obj = Instantiate (myPrefab, position, rotation, parent) as GameObject;
// The you can do with the newly spawned object what you want
obj.GetComponent<BehaviorName> ().DoSomething ();
Your answer
Follow this Question
Related Questions
Cost of loading manually modified instances at runtime, on mobiles? 1 Answer
Passing array of prefabs to next scene 1 Answer
How (and where) does Unity save the modified parameters of a prefab's instance? 1 Answer
Delete prefab in resource folder 0 Answers
How to do not apply fields in prefab 1 Answer