Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Linghiie · May 04, 2013 at 09:26 PM · instantiateprefabgetcomponentdoor

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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;
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Linghiie · May 05, 2013 at 03:47 PM 0
Share

What do you mean by "in your function"?

Again, sorry but I'm fairly new to coding.

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Linghiie · May 05, 2013 at 03:45 PM 0
Share

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.

avatar image Dr.Z · May 05, 2013 at 04:36 PM 0
Share

Yes that's what I meant. By doing this, your script doesn't call GetComponent() every frame (because Update() is called every frame).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get Component from Instantiated Prefab 1 Answer

How to reduce usage of GetComponent()? (instantiate, prefabs) 0 Answers

Accessing prefab data before instantiation breaks prefab? 3 Answers

Referencing in a prefab 1 Answer

Get unity to recognize prefab in C# 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges