• 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 /
This question was closed Mar 23, 2019 at 02:27 PM by pako for the following reason:

The question is answered, right answer was accepted

avatar image
Question by Jithamanyu · Mar 28, 2017 at 04:49 PM · errorcolliderobjectduplicate

duplicated object destroyed when original object destroyed

hi...i am new to unity...i have a banana prefab tagged as "ban". now have a problem with collider that is a trigger. i written a score system program on it and it works good.but, the problem is that i duplicated the object which has same tag.when i collide with my player the original of the banana prefab increases the score..but not duplicated prefabs increasing it.
i am using this script on that prefab :

 public void OnTriggerEnter(Collider hit)
     {
         if (hit.gameObject.CompareTag("player")) 
         {
             sc += 5.0f;
             score_value.text = ((int)sc).ToString ();
             Destroy (this.gameObject);
         }
     }

Is there any solution for this? (please..it's urgent :( )!!!

Comment

People who like this

0 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 Jithamanyu · Mar 28, 2017 at 01:36 PM 0
Share

sorry..that i am not well knowledged person in the way of speaking English and Programming.

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by meat5000 · Mar 28, 2017 at 05:06 PM

You check for Player tag on hit object but then proceed to destroy the GO attached to 'this' which refers to the script you are reading. So the GO the script is on is destroyed.

Try hit.gameObject instead of this.gameObject if thats what you are trying to achieve.

For all objects to update the same scoreboard you need to grab the actual scoreboard with GetComponent to access the same information.

Comment
Avin

People who like this

1 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 Jithamanyu · Mar 29, 2017 at 05:00 PM 0
Share

@meat5000 :) Oh....Ok.....it's working now...i made empty object as child of my player. and made that same script as you said..Thanks Man.... :)

avatar image NoseKills Jithamanyu · Mar 30, 2017 at 04:54 PM 0
Share

If the answer is correct, please accept it with the checkmark button near the answer's score.

avatar image Raymond_Michael_Rodrigo · Mar 23, 2019 at 02:26 PM 0
Share

Thank you. Yeah, That worked for me, it also in the Unity documentation too - later I found it there

https://docs.unity3d.com/ScriptReference/Component.CompareTag.html

 // Immediate death trigger.
 // Destroys any colliders that enter the trigger, if they are tagged player.
 using UnityEngine;
 using System.Collections;
 
 public class ExampleClass : MonoBehaviour
 {
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag("Player"))
         {
             Destroy(other.gameObject);
         }
     }
 }


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

6 People are following this question.

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

Related Questions

Concave collider error 0 Answers

Picked up objects passes through objects 2 Answers

Error[Component CircleCollider2D could not be loaded when loading game object] 0 Answers

Internal collisions 1 Answer

Object reference not set to an instance of an object. 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