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 tas41 · Sep 04, 2016 at 09:51 AM · c#gameobjectrigidbodydestroydragrigidbody

C# destroy gameobject with tag

I'm making a script that creates a empty game object in the center of a rigid body with a specific tag, then destroys it when the mouse is lifted. Everything but the destruction part seems to work fine, so what's wrong?

 GameObject dragger;
 private Camera fpsCam;
 public float range;


 // Use this for initialization
 void Start () {
     fpsCam = GetComponent<Camera>();

 }
 
 // Update is called once per frame
 void Update () {

     if(Input.GetButtonDown ("Fire1"))
     {
         Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
         RaycastHit hit;
         if(Physics.Raycast (rayOrigin,fpsCam.transform.forward, out hit, range))
         {
             if (hit.transform.tag == "Grabbable")
             {
                 Debug.Log("hit:" + hit.rigidbody);
                 dragger = new GameObject("dragger");
                 dragger.transform.position = hit.transform.position;
                 dragger.transform.parent = fpsCam.transform;
                 dragger.gameObject.tag = "dragger";
                 
             }
             
         }
         else
         {
             Debug.Log("not hit");

         }                if (GameObject.FindWithTag ("dragger")&& Input.GetButtonUp("Fire1"))
                 {
                 Destroy(GameObject.FindWithTag("dragger"));

                 }

              
                     
                 }
     }
 }
Comment
Add comment · Show 1
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 iFallOffStuff · Sep 10, 2016 at 08:35 AM 0
Share

Do you get any kind of error message?

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by millej23 · Sep 06, 2016 at 04:42 AM

Looking at it quickly, try using:

 if(Input.GetButton("Fire1"))

I believe the GetButtonDown will trigger once per press, while GetButton will trigger while the mouse is held down. If you are trying to check while holding the mouse or fire down, try GetButton to have the ray check while held down. Let me know if that helps, thanks!

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 tas41 · Sep 10, 2016 at 03:29 AM 0
Share

Unfortunately, it's not what i'm looking for, i'm trying to destroy the dragger when the player lets go. Thanks for giving an interest though.

avatar image
0

Answer by Acid_kenobi · Sep 05, 2016 at 10:33 PM

Pretty sure you could do:

  if (dragger != null && Input.GetButtonUp("Fire1")){
     Destroy(dragger );
     dragger = null;
 }

Sorry, its early. Hope this helps :)

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 tas41 · Sep 10, 2016 at 03:32 AM 0
Share

unfortunately it didn't help, and it's weird because i even made it check before it destroys too. so i guess the problem has to be with the destruction part of it?

  if(Input.GetButtonUp("Fire1"))
             {
                 getDragger = GameObject.FindWithTag("dragger");
                 Destroy(getDragger);
                 
             }
avatar image
0

Answer by tas41 · Sep 10, 2016 at 08:37 AM

I see now, i think the problem was just a misplaced bracket, oops Here's the finished code :

 using UnityEngine;
 using System.Collections;

 public class Raycast : MonoBehaviour {
 GameObject dragger;
 private Camera fpsCam;
 public float range;
 public GameObject getDragger;
 // Use this for initialization
 void Start () {
     fpsCam = GetComponent<Camera>();
 }
 
 // Update is called once per frame
 void Update () {
     getDragger = GameObject.FindWithTag("dragger");
     if (Input.GetButtonDown("Fire1"))
     {
         Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));


         RaycastHit hit;


         if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, range))
         {

             if (hit.transform.tag == "Grabbable")
             {
                 Debug.Log("hit:" + hit.rigidbody);
                 dragger = new GameObject("dragger");
                 dragger.transform.position = hit.transform.position;
                 dragger.transform.parent = fpsCam.transform;
                 dragger.gameObject.tag = "dragger";

             }

         }
         else
         {
             Debug.Log("not hit");

         }
     }         
         if(Input.GetButtonUp("Fire1"))
         {
             getDragger = GameObject.FindWithTag("dragger");
             Destroy(getDragger);
             
         }
              
                                        
     }
 }

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 tqkiettk10 · Sep 10, 2016 at 03:51 AM

I think your block code "{}" is wrong so I try to fix it:

  GameObject dragger;
      private Camera fpsCam;
      public float range;
      // Use this for initialization
      void Start () {
          fpsCam = GetComponent<Camera>();
      }
      
      // Update is called once per frame
      void Update () {
          if(Input.GetButtonDown ("Fire1"))
          {
              Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
              RaycastHit hit;
              if(Physics.Raycast (rayOrigin,fpsCam.transform.forward, out hit, range))
              {
                  if (hit.transform.tag == "Grabbable")
                  {
                      Debug.Log("hit:" + hit.rigidbody);
                      dragger = new GameObject("dragger");
                      dragger.transform.position = hit.transform.position;
                      dragger.transform.parent = fpsCam.transform;
                      dragger.gameObject.tag = "dragger";
                      
                  }else
              {
                  Debug.Log("not hit");
              } 
           }
                    if (GameObject.FindWithTag ("dragger")&& Input.GetButtonUp("Fire1"))
                      {
                      Destroy(GameObject.FindWithTag("dragger"));
                      }
          }
      }
 
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

8 People are following this question.

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

Related Questions

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

GameObject.FindGameObjectsWithTag still finding destroyed object (C#) 1 Answer

Particle system not destroying. 3 Answers

How to only delete one of two collided objects? 1 Answer

Store Game Object Into List For Later Reinstantiation? 0 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