• 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 kobal · Jan 11, 2013 at 01:12 AM · instantiate prefab

Instantiate prefab OnTriggerEnter

I would like to Instantiate a rock prefab to fall from the sky when you enter a trigger, then be destroyed after a few seconds.

I am having trouble getting it to work though. I have made a prefab rock with rigid body. I have made a collider marked as trigger and added this java script.

 var rock : GameObject;
 
     function OnTriggerEnter (other : Collider) 
     {
         var clone : GameObject = Instantiate (rock, transform.position, transform.rotation);
                 Destroy(gameObject);
     }
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
0

Answer by kobal · Jan 11, 2013 at 03:46 AM

Figured it out. I was dumb and had the script on both the Rock and the Trigger.

Comment
Add comment · 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
0

Answer by DaveA · Jan 11, 2013 at 01:18 AM

What you have, assuming it's on the collider object, will destroy that collider when you enter it, just making sure you want to do that.

It will also put the rock in the same place that the collider object was. My guess is you want to put it a few unit above that? Like:

Instantiate (rock, transform.position+Vector3.up*3, transform.rotation);

Comment
Add comment · Show 5 · 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 kobal · Jan 11, 2013 at 01:27 AM 0
Share

Hey Dave. Yes that is correct. I do not want the rock to fall every time you enter the collider, just once. You are also correct on having the rock spawn higher than the collider. Do you have any idea why this is not working? The error I get is "UnassignedReferenceException: the variable rock of 'fallingRock' has not been assigned." I did put the rock prefab on the script in the inspector.

avatar image kobal · Jan 11, 2013 at 01:36 AM 0
Share

BTW it almost works. I see the rock spawn and start to fall. Then I get the error.

avatar image DaveA · Jan 11, 2013 at 01:38 AM 0
Share

Is 'fallingRock' the name of the above script? I'd say you forgot to assign it in the Inspector. If you have more than one of these in the scene, be sure to do it for all of them.

avatar image kobal · Jan 11, 2013 at 01:50 AM 0
Share

Yes. fallingRock is the name of the script. I guess I am confused on how to assign it. I thought I did. I put the script on the game object that is the trigger. I then put the rock prefab on the script.

avatar image kobal · Jan 11, 2013 at 02:21 AM 0
Share

I figured out what is causing the error, but I dont know how to fix it. The rock spawns and falls as intended. When the falling rock hits the collider I am using as a trigger I get the error.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

Problem instantiating a prefab, and storing a reference to it. 1 Answer

How do I spawn a random unit from my XML file? 0 Answers

aim instantiated GUI text at camera 0 Answers

When attempting to place an object from prefab, the object continually jumps up to the camera and back down quickly. 0 Answers

Argument Exception: The Object you want to instantiate is null (A new one?) 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