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 ReynaV · Nov 04, 2013 at 01:44 AM · destroygameobject

OnMouseDown - if statement

Hello. I hope you can help me. I am trying to disappear an object when I click another one. I want to distinguish between two different objects. This is my code, but it doesn't work out.

     public GameObject boyGP2, dogGP2;
     
 
     void Update () {
     
     }
     
     void OnMouseDown(){
         if(gameObject.name=="boyGP2"){
             Destroy(dogGP2);
         }
         if(gameObject.name=="dogGP2"){
             Destroy(boyGP2);
         }
     }
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 Cains · Nov 04, 2013 at 02:15 AM

Some things to check:

Is this script attached to both GameObjects in the editor? Otherwise the script isn't running

In order for this to work you'd have to go to each GameObject's inspector in the editor and fill the two variables with GameObjects. Otherwise the dogGP2 and boyGP2 variables will be blank.


Another way to solve the second tip is to define the variables at scene start like below:

 // Attach this script to both objects
 
 public GameObject boyGP2, dogGP2;
 
 void Start() {
     boyGP2 = GameObject.Find("boyGP2");
     dogGP2 = GameObject.Find("dogGP2");
 }
 
 void OnMouseDown() {
        if(gameObject.name=="boyGP2"){
          Destroy(dogGP2);
        }
        if(gameObject.name=="dogGP2"){
          Destroy(boyGP2);
        }
 }

I always code in UnityScript, but I'm pretty sure I got something as simple as this right.

Comment
Add comment · Show 2 · 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 ReynaV · Nov 04, 2013 at 02:39 AM 0
Share

I've added the code to a terrain object; also I've used the inspector. First, I tried to associate a single code to each gameObject but it didn't work, too.

avatar image Cains · Nov 04, 2013 at 02:48 AM 0
Share

If you're attaching this script to a terrain, realize that the On$$anonymous$$ouseDown() will only be called when the player clicks on the terrain, and not the objects. And since I'm assu$$anonymous$$g the terrain's name is neither boyGP2 or dogGP2 the On$$anonymous$$ouseDown() function runs but doesn't do anything whenever you click the terrain.

The On$$anonymous$$ouseDown() function is called whenever the player clicks on the object the script is on, in this case the terrain.

If you attach the script I posted above to both GameObjects ins$$anonymous$$d and not the terrain I believe it will work as you expect it to.

avatar image
0

Answer by alok1974 · Nov 04, 2013 at 03:08 AM

First, make sure you have colliders on your game objects or else OnMouseDown() will not be called.

This script should be able to take care of it. Just attach it on both the game objects.

 using UnityEngine;
 using System.Collections;
 
 public class DestroyOther : MonoBehaviour
 {
     private GameObject other;
     
     void Start ()
     {
         switch (gameObject.name) {
         case "boyGP2":
             other = GameObject.Find ("dogGP2");
             break;
         
         case "dogGP2":
             other = GameObject.Find ("boyGP2");
             break;
             
         }
     
     }
     
     void OnMouseDown ()
     {
         Destroy (other);
     }
 
 }



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

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

Related Questions

How do I destroy my player on contact? 2 Answers

OnMouseDown() function 0 Answers

Enemy does not destroy player in 2D 1 Answer

How do I destroy an object after giving the player a point? 2 Answers

How do i get my object to explode and destroy? 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