- Home /
How to instantiate a prefab after a certain action happened?
So I'm making this horror game and I am trying to make it so that when a certain door opens, something spawns for a very brief moment at the door.
So for the door i am going to use a boolean variable that keeps track of whether or not the door is open, using if statements.
But for the object that spawns i am not really sure what to do. I am thinking about an if statement like this:
if (GetComponent("DoorScript").isDoorOpen = true) { instantiate the prefab }
What should be where "instantiate the prefab" is now?
Thanks in advance and i hope i explained it in enough detail.
Answer by AlucardJay · May 04, 2013 at 10:10 PM
Ok, I am going to give you this one, as it can be confusing how to only make something happen once in a game lifetime, and you have shown effort in thinking about the problem.
You have the right idea with a boolean, so lets start there :
var hasShownDoorPopup : boolean = false;
now GetComponent is quite heavy on the processor, so let's first check if the door popup has been done :
// note the ! this is the same as saying if ( hasShownDoorPopup == false )
if ( !hasShownDoorPopup )
{
//
}
so now the DoorScript can be checked. Remember to use == in a conditional (you only have one equals in your example if statement). Also try not to use strings when using GetComponent, just typecast to the name of your script :
if ( GetComponent( DoorScript ).isDoorOpen == true ) { // .... }
After that, instantiate your prefab , and then set hasShownDoorPopup to true. Now this part of the code will not be executed again in the current game lifetime.
Here is a full example :
var hasShownDoorPopup : boolean = false;
// in your function
if ( !hasShownDoorPopup )
{
if ( GetComponent( DoorScript ).isDoorOpen == true )
{
// instantiate prefab
// set boolean to true so this doesn't happen again
hasShownDoorPopup = true;
}
}
What do you mean by "in your function"?
Again, sorry but I'm fairly new to coding.
Answer by Dr.Z · May 04, 2013 at 10:03 PM
You almost answered yourself : Use the Instantiate function Something like:
Instantiate(YourPrefab, new Vector3(X, Y, Z), [Quaternion.identity][1])
[1]: http://docs.unity3d.com/Documentation/ScriptReference/Quaternion-identity.html?from=Object
A little advice. If you're using this line "if (GetComponent("DoorScript").isDoorOpen = true)" in an Update() function, you should consider storing it before (may be in the Start() function, because GetComponent is slow.
So would it be a smart idea to save the "isDoorOpen" variable as another variable in the start function and then check if its true or not in the update function? it would look something like this:
function Start(){ var isDoorOpen2 = GetComponent("DoorScript").isDoorOpen; }
function Update() { if isDoorOpen2 = true { Instantiate($$anonymous$$yPrefab, new Vector3(X, Y, Z), [Quaternion.identity][1]) }
Sorry if this doesnt make sense but i'm fairly new to scripting.
Yes that's what I meant. By doing this, your script doesn't call GetComponent() every frame (because Update() is called every frame).