Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 pacocloud · Jun 02, 2016 at 04:00 PM · colliderdestroy object

Destroy Gameobject on collision

So I have a controller script where I only want to destroy the game object when it matches tags and I happen to be using an ability. This ability is slam. However even though I have it programmed to need both conditions in order to destroy, the platform gets destroyed whether im using slam or not. no matter what. Please help.

using System; using UnityEngine; using System.Collections; using UnityEngine.UI;

namespace UnityStandardAssets.Vehicles.Ball { public class Ball : MonoBehaviour { // variables public float basespeed; public float jumppower; public float sprintspeed; public float currentspeed; public float slampower;

     private Rigidbody rb;
     private const float k_GroundRayLength = .8f; // The length of the ray to check if the ball is grounded.
     public bool slam;
     public bool grounded; 
     //function start
     void Start ()
     {
         rb = GetComponent<Rigidbody> ();
     }
     
     public void Move(Vector3 moveDirection, bool jump){
 
         {
             // basic movement
             rb.AddForce (moveDirection * currentspeed);
         }
     }
     void Update (){
     {
         //jumping sequence
         if (Physics.Raycast (transform.position, -Vector3.up, k_GroundRayLength) && (Input.GetKeyDown (KeyCode.Space)))
             rb.AddForce (new Vector3 (0, jumppower, 0));
     }    
     {    //sprint sequence
             if (Physics.Raycast (transform.position, -Vector3.up, k_GroundRayLength) && (Input.GetKey (KeyCode.LeftShift)))
             currentspeed = sprintspeed;
         else 
             currentspeed = basespeed;
     }
         {
             if (Physics.Raycast (transform.position, -Vector3.up, k_GroundRayLength))
                 grounded = true;
             else 
                 grounded = false;
         }
     //Slam Sequence
     {
             if (Input.GetKeyDown (KeyCode.Q) && (grounded == false)) 
                 slam = true;
             {
                 if (slam == true)
                     rb.AddForce (new Vector3 (0, slampower, 0));
             }
     }
 }
     void OnTriggerEnter(Collider collider)
     {
         {
             if ((slam == true) && (collider.gameObject.tag == "Destroy"))
                 Destroy (GameObject.FindGameObjectWithTag ("Destroy"));
         }
         {
             if (slam == true)
                 slam = false;
         }
     }
 }

}

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 Grimmick · Jun 03, 2016 at 10:01 PM

Are you sure, slam is false when you think you did not use slam? As the slam field is public another script could have modified it or maybe you set the value to true in the editor. To be 100% sure you could temporarily replace slam by a private variable or use Debug.Log at the beginning of OnTriggerEnter to print the value of slam.

Are you sure, the platform is destroyed by the call to Destroy in OnTriggerEnter? So it's not destroyed anymore when you comment out the code in OnTriggerEnter? Maybe another script causes the platform to be destroyed.

Why do you destroy "one active GameObject tagged" instead of the game object that entered the trigger? Why don't you directly use collider.gameObject? Using GameObject.FindGameObjectWithTag (which does not appear in the documentation, but checking UnityEngine.dll shows that FindWithTag just calls GameObject.FindGameObjectWithTag) may lead to a different platform being destroyed.

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 $$anonymous$$ · Jun 04, 2016 at 10:20 AM

 void OnTriggerEnter(Collider iWantToDestoryYou)
      {
          {
              if ((slam == true) && (collider.gameObject.tag == "Destroy"))
                  Destroy (iWantToDestoryYou); //iWantToDestoryYou is gameobject which collided so it will destroy him
          }
          {
              if (slam == true)
                  slam = false;
          }
      }
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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Colliding Objects 1 Answer

Is it possible to destroy an object only within the collider? 1 Answer

Multiplayer: I have an OnTrigger script that isn't correctly responding. Spawning and Destroying GameObjects. 0 Answers

2d edge collider wont destroy? 0 Answers

How can I destroy an object if it touches NOTHING 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