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 Jamie_h88 · Oct 15, 2013 at 09:46 AM · collisiongameobjectinstantiatevector3destroy

Instantiate after collision

Hi, for some reason my code wont Instantiate a new gameobject after I hit it. Can someone help?

 var scriptName : GameObject;
 var target: GameObject;
 
 
 function OnCollisionEnter(theCollision : Collision){
  
  
  
  // collision for ball destroys object
 if(theCollision.gameObject.name == "BasketBall(Clone)")
 {
 
     scriptName.GetComponent(Jh_Score_Script).addsometoscore();
     Destroy(gameObject);
     yield WaitForSeconds(1);
     Instantiate (target, Vector3(0,1,0), Quaternion.identity);
     }
     
 }
     
Comment
Add comment · Show 4
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 Doireth · Oct 15, 2013 at 09:50 AM 0
Share

Does the object you have this script attached to have a rigidbody component?

avatar image Jamie_h88 · Oct 15, 2013 at 10:13 AM 0
Share

I have attached to a an empty gameobject and put a target prefab as the gameobject variable. I as you can tell i'm trying to get one target to destroy on collision and instantiate another.

avatar image mattssonon · Oct 15, 2013 at 10:17 AM 0
Share

Try doing this:

 gameObject.renderer.enabled = false;
 yield WaitForSeconds(1);
 Instantiate (target, Vector3(0,1,0), Quaternion.identity);
 Destroy(gameObject);
avatar image Doireth · Oct 15, 2013 at 10:17 AM 0
Share

OnCollisionEnter only works if there is a rigidbody attached. See Documentation

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Doireth · Oct 15, 2013 at 10:40 AM

For collision to occur your object needs a Collider and a Rigidbody. Without both of those "OnCollisionEnter" never triggers.

Comment
Add comment · Show 3 · 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 mattssonon · Oct 15, 2013 at 09:54 AM 0
Share

You use Destroy() meaning that the object is destroyed after the current Update loop, so code delayed with a whole second will never be run, because the object and the component no longer exists then.

avatar image Jamie_h88 · Oct 15, 2013 at 10:59 AM 0
Share

I have this. The collision occurs on both instances. The script now instantiates one target and then still detects collision but will not destroy or instantiate another target.

 var scriptName : GameObject;
 var target: GameObject;
 
 
 
 function OnCollisionEnter(theCollision : Collision){
 
 Debug.Log("Have I hit anything");
  
  
  // collision for ball destroys object
 if(theCollision.gameObject.name == "BasketBall(Clone)")
 {
 
 gameObject.renderer.enabled = false;
 Instantiate (target, this.transform.position, Quaternion.identity);
 Destroy(gameObject);
 scriptName.GetComponent(Jh_Score_Script).addsometoscore();
 Debug.Log("Have I hit basketball");
 yield WaitForSeconds(3);
 
     
     }
     
 }
     
     

 
avatar image Jamie_h88 · Oct 15, 2013 at 03:53 PM 0
Share

This method just did not work so i made a trigger script.

 // plug in score script
 var scriptName : GameObject;
 // plug in tartget to instantiate
 var target : GameObject;
 //plug in audio clip
 var explosion : AudioClip;
 // sets int for basket
 var wentThroughBasket = 0;
 
 
 function OnTriggerEnter (myTrigger : Collider) 
 {
 // checks for collision with basket ball
     if(myTrigger.gameObject.name == "BasketBall(Clone)")
     {
         scriptName.GetComponent(Jh_Score_Script).addsometoscore();    
         wentThroughBasket++;
 
         var targets : GameObject = Instantiate(target,Vector3(-88,6,1), Quaternion.identity);
 
             if(wentThroughBasket == 3)
             {
                  WaitForSeconds (4);
                  Application.LoadLevel(5);
             }
     }
 }

I then attached a collision script to the prefab.

// variable for sound var explosion : AudioClip;

// Collision Detection function OnCollisionEnter(theCollision : Collision){

// collision for ball destroys object if(theCollision.gameObject.name == "BasketBall(Clone)") { // destorys target Destroy(gameObject); }

     if(theCollision.gameObject.name == "Target")
     {
         //plays audio
          audio.PlayOneShot(explosion);
      }

}

avatar image
0

Answer by Jamie_h88 · Oct 15, 2013 at 10:09 AM

I have changed the Destroy command to Destroy(target), would this not be away around this?

Comment
Add comment · Show 3 · 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 mattssonon · Oct 15, 2013 at 10:13 AM 2
Share

Please post comments by clicking Add new comment under my answer ins$$anonymous$$d of posting a new answer. :) That depends on what you're trying to destroy. You chould do this if you just want the Game Object to disappear on collision:

 gameObject.renderer.enabled = false;
 yield WaitForSeconds(1);
 Instantiate (target, Vector3(0,1,0), Quaternion.identity);
 Destroy(gameObject);
avatar image mattssonon · Oct 15, 2013 at 10:35 AM 0
Share

Is the collision code never run?! Do you have a Rigid Body component on the Game Object this script is on? Are you sure the object you're colliding with is called "BasketBall(Clone)"?

avatar image mattssonon · Oct 15, 2013 at 10:42 AM 0
Share

Try adding Debug.Log(theCollision.gameObject.name); after function OnCollisionEnter(theCollision : Collision){ to see if the name of the Game Object fits with the name in the if conditional.

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

15 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 avatar image

Related Questions

Having problems with destroying, instantiating 0 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

what to put in vector3 when instantiating Gameobject at Gameobject? 2 Answers

Collision Detection for a Prefab? 3 Answers

Instantiate and Destroy an object while crossing a line with its clone also 1 Answer


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