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 LeathalNinja1986 · Aug 22, 2012 at 04:12 AM · objectdestroyclick

Destroying specific clone object while not destroying others on screen

I am making a game and I have run into another command problem, cloned characters spawn and are destroyed with mouse click, however they are all destroyed when I click no matter where the mouse is pointing. I have tried specific location codes I have found but they do not destroy the characters when saved in script.

 function Update(){
     if (Input.GetMouseButtonDown(1)){
     ScoreLives.score += 150;
     Instantiate(poof, transform.position, transform.rotation);
     Destroy(gameObject);
     }
 }

ScoreLives.score += 150; Instantiate(poof, transform.position, transform.rotation); Destroy(gameObject); } }

---This code destroys them, but its a mass kill command to all the clone characters with each click.

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 Khada · Aug 22, 2012 at 01:56 PM 0
Share

Your last 5 questions haven't been marked as answered, if people notice this they may be reluctant to help. Please go through your questions (viewable from your profile page) and mark the appropriate answers.

(Questions can be marked as answered by clicking the checkmark below the up/down vote buttons on any answer.)

5 Replies

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

Answer by LeathalNinja1986 · Aug 27, 2012 at 11:57 PM

 var clicked : boolean = false;
 var hit : RaycastHit;
 
 
 function Update() {
     if(Input.GetMouseButtonDown(0) &&
        collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit,
                         Mathf.Infinity)) {
         clicked = !clicked;
         if(tag == "Selected") tag = "Unselected";
         else tag = "Selected";
         if(tag == "Unselected")
         ScoreLives.score += 150;
         Instantiate(poof, transform.position, transform.rotation);
         Destroy(gameObject);
         Destroy(poof);
     }
 }
 Found a new method to recognize individual clones, using tag switching a boolean 

``and the raycast system for screen location. Thanks for help everyone I would have been able to recognize the solution without so many different options to learn and draw from.

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
1

Answer by Seth-Bergman · Aug 22, 2012 at 04:32 AM

what do you want to happen? if you only want to destroy the object clicked, you could try OnMouseDown:

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnMouseDown.html

otherwise how do you want to choose which one is destroyed?

EDIT: I had only meant to leave a comment, but since I accidentally put this as an answer, guess I'll go ahead and elaborate :)

the reason every object is being destroyed is because every object has the code:

  if (Input.GetMouseButtonDown(1)){
     Destroy(gameObject);
     }

therefore, on every mouse click, every object with this script destroys itself..

if you instead say:

 function OnMouseDown (){
     ScoreLives.score += 150;
     Instantiate(poof, transform.position, transform.rotation);
     Destroy(gameObject);
 }

it will only affect the object actually clicked (only works with the left button tho).. but if you want to use the right mouse button, you can try:

 function OnMouseOver () {
    if (Input.GetMouseButton(1)) {
       // etc...
    }
 }

this is all assuming you want to destroy the object which is clicked.. But if it was some other factor that would determine the object to destroy, you would likely want a single controlling script checking all your clones and finding the right one to destroy... for example: (on only one object in the scene)

 function Update(){
 
      if (Input.GetMouseButtonDown(1)){ // on right click..
 var enemies : GameObject[] = GameObject.FindGameObjectsWithTag("Enemy"); // find all enemies in scene..
 
 for(var i = 0; i < enemies.Length; i++){ // for loop to cycle through list of all enemies
 var enemyScript = enemies[i].GetComponent(EnemyScript); // example..
 if(enemyScript.health < 2){ // condition to check..
 Destroy(enemies[i]); //destroy the appropriate object(s)
       }
     }
   }
 }

or at least you will want some additional requirement to meet to reach the destroy code.. for example: (on each object in the scene [as before])

 function Update(){
     
 var other : Transform = GameObject.Find("Player").GetComponent(Transform);
 
 if (other) {
     var dist = Vector3.Distance(other.position, transform.position);
 
      // only destroy self if within a certain range of player
     if (Input.GetMouseButtonDown(1) && dist < 10){  
     ScoreLives.score += 150;
     Instantiate(poof, transform.position, transform.rotation);
     Destroy(gameObject);
     }
   }
 }
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 SvenEV · Aug 22, 2012 at 01:50 PM

Seth Bergman is right. Input.GetMouseButtonDown(1) is true when the right mouse button is pressed. It does NOT check whether the Cursor is actually on your game object.

You could use the OnMouseDown handler but that only triggers on left clicks:

 function OnMouseDown()
 {
     ScoreLives.score += 150;
     Instantiate(poof, transform.position, transform.rotation);
     Destroy(gameObject);
 }

However, as described in the last post of this topic, you can do it like this do detect a right click (OnMouseOver() gets triggered on the GameObject the cursor points on. Then you check for the right mouse button):

 function OnMouseOver()
 {
     if (Input.GetMouseButtonDown(1))
     {
         ScoreLives.score += 150;
         Instantiate(poof, transform.position, transform.rotation);
         Destroy(gameObject);
     }
 }


Comment
Add comment · Show 4 · 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 Khada · Aug 22, 2012 at 01:53 PM 0
Share

Actually Input.Get$$anonymous$$ouseButtonDown(1) checks if the R$$anonymous$$B is pressed.

avatar image SvenEV · Aug 22, 2012 at 02:19 PM 0
Share

oh, I'm sorry.

avatar image Khada · Aug 22, 2012 at 02:20 PM 0
Share

No need to be sorry, just letting you know.

avatar image SvenEV · Aug 22, 2012 at 02:25 PM 0
Share

fixed it.

avatar image
0

Answer by Arshia001 · Aug 22, 2012 at 12:18 PM

Judging from your code, it's placed on the clones, right? If that is the case, every one of the clones is checking the mouse button and destroying itself. It is VERY VERY bad practice to write Input code in different places, since problems like this might arise. What you must do is move all of your input code to one single class. In that class, you must check Input.GetMouseButtonDown as you do now, and if the mouse was indeed clicked this frame, you must find out which clone the mouse is pointing at (see below) and tell that specific clone to destroy itself. Your code should look like this in the end:


void Update()
{
if (Input.GetMouseButtonDown(1)){
Ray r = Camera.main.ScreenPointToRay(input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(r, out hit)){
Clone c = hit.gameObject.GetComponent<Clone>(); //I'm assuming you have a script called Clone which is attached to the clones.
if (c != null)
c.Annihilate(); //and that the Clone script has a function called Annihilate which handles spawning the "poof" and destroying itself.
}
}
}

If you don't know the functions I used above, look them up in the Unity doc. Also, I wrote this code from memory, so forgive any possible misspellings plz :D

Comment
Add comment · Show 4 · 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 aldonaletto · Aug 22, 2012 at 12:26 PM 0
Share

@Arshia001, this damned UA code preprocessor has eaten the GetComponent type (which I restored): it thinks that every < character starts a tag, and swallows the text until a closing > is found. You can avoid this by adding a blank space right after < - at least this trick is still working (hope they don't change this too...)

avatar image Arshia001 · Aug 22, 2012 at 12:56 PM 0
Share

I hadn't realized it! Tnx!

avatar image Arshia001 · Aug 22, 2012 at 01:14 PM 0
Share

A bit of messing around reveals that the official way to get < and > correct is to type them as &gt; and &lt; which in turn means that I typed the "&gt;" in my response as "&amp;gt;", and so on...

avatar image LeathalNinja1986 · Aug 23, 2012 at 09:16 PM 0
Share

I trying a slight variation of this code but what do the void even mean? I'm getting faults that say the semicolons are not there when they are, any thoughts as to why this phantom error keeps popping up?

function Update() {

if(Input.GetButtonDown("0")){

Raycast ray = Camera.main.ScreenPointToRay(Input.$$anonymous$$ousePosition);

Raycasthit hit;

if(Physics.Raycast(ray, hit))

Destroy(GameObject.Find(hit.chicken));

Instantiate(poof, transform.position, transform.rotation);

Will this fix the problem I've been having that I posted, if the errors are solved?

avatar image
0

Answer by aldonaletto · Aug 22, 2012 at 12:21 PM

Man, this is a mass suicide script! Every object with this script will suicide when the right button is pressed. @Seth Bergman's suggestion would be the best alternative if you were reading the left mouse button, but unfortunately the right button doesn't generate OnMouseDown events - thus you must use raycast to select the victim. A simple way to do that is to find the clicked enemy with the code below (attached to the camera) and call the enemy suicide code with SendMessage:

Camera Script:

function Update(){
  if (Input.GetMouseButtonDown(1)){
    var ray = camera.ScreenPointToRay(Input.mousePosition);
    var hit: RaycastHit;
    if (Physics.Raycast(ray, hit)){
      // call the function DieBastard (if any) in the clicked object:
      hit.transform.SendMessage("DieBastard", null, SendMessageOptions.DontRequireReceiver);
    }
  }
}
Enemy script:

function DieBastard(){
  ScoreLives.score += 150;
  Instantiate(poof, transform.position, transform.rotation);
  Destroy(gameObject);
}
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 LeathalNinja1986 · Aug 27, 2012 at 04:18 AM 0
Share

NullReferenceException On Camera.Update () (at Assets/Standard Assets/Character Controllers/Sources/Scripts/On Camera.js:11) ---This is the error I'm getting when I apply your script, how would I resolve it?

avatar image aldonaletto · Aug 28, 2012 at 12:57 PM 0
Share

The first script must be attached to the camera, or you'll get Null Reference errors (the property camera is null unless the object has a Camera component)

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

11 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

Related Questions

Destroy object using mouse click(raycast to detect collision) 2 Answers

Destroy objects by clicking on them 1 Answer

Destroy on touch for iphone 1 Answer

Destroy an enemy when it touches an object 2 Answers

Object won't instantiate at parents position(solved) 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