- Home /
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.
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.)
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.
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);
}
}
}
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);
}
}
Actually Input.Get$$anonymous$$ouseButtonDown(1) checks if the R$$anonymous$$B is pressed.
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
@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...)
A bit of messing around reveals that the official way to get < and > correct is to type them as > and < which in turn means that I typed the ">" in my response as "&gt;", and so on...
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?
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); }
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?
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)