x


Objects with shared tags, delete currently selected?

I want to delete the game object I'm looking at, I've set it a tag "Paper" but because I have multiple objects with the same tag it seems to delete them in sequence and not the one I'm staring at... How can I fix this?

var PageCount : GUIText;

function Update () {

if ( Input.GetMouseButtonDown(0) )
   {
    var hit : RaycastHit;
    var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
      if (Physics.Raycast (ray, hit, 0.8))
      {
         Destroy(GameObject.FindWithTag("Paper"));
         PageCount.guiText.text = PageCount.guiText.text + "O";
      }
   }

}
more ▼

asked Oct 12 '12 at 01:33 AM

Konkor4 gravatar image

Konkor4
11 3 10

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

I asked for something similar two days ago, try this:

  function Update(){

  var hit : RaycastHit;

var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

    if (Physics.Raycast (ray, hit, 200))
      {

         hit.transform.SendMessage("Touched",hit, SendMessageOptions.DontRequireReceiver);


 }
 }

With this you don't require a tag to hit the object, just create a script with the "Touched" function and add it to every object you want to get destroyed, add inside that function whatever you want to happen if the obj is clicked, then add this code to the camera. Hope it helps :)

more ▼

answered Oct 12 '12 at 02:35 AM

Noah 1 gravatar image

Noah 1
1.1k 33 45 50

Thanks, I added it to my script and it seems to work, but how can I make it delete the object I touch? I am only trying to remove 1 type of object but I have several placed...

Oct 12 '12 at 04:16 AM Konkor4

Did you added the code with the touched function to your objects?

function Touched(hit:RaycastHit){Destroy (gameObject);}

Sorry if i have some mistakes, im on ipad...

Oct 12 '12 at 04:25 AM Noah 1

I've just set this as my code:

[code] var PageCount : GUIText;

function Touched(hit:RaycastHit) { Destroy (gameObject); }

function Update () { if ( Input.GetMouseButtonDown(0) ) { var hit : RaycastHit; var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition); if (Physics.Raycast (ray, hit, 0.8)) { hit.transform.SendMessage("Touched",hit, SendMessageOptions.DontRequireReceiver); PageCount.guiText.text = PageCount.guiText.text + "O"; } } } [/code]


Now when I start it up, I have 2 planes with the Paper tag. These are the two objects I want to delete in any order, but I can still only delete them in a certain order. I dont think I'm making sense since it's 5am but basically what I want is to be able to have about 17 copies of the same object and be able to delete them 1 at a time in any order.

Oct 12 '12 at 04:34 AM Konkor4
(comments are locked)
10|3000 characters needed characters left
      if (Physics.Raycast (ray, hit, 0.8))
      {
         if (hit.transform.gameObject.tag == "Paper")
           Destroy(hit.transform.gameObject);
more ▼

answered Oct 12 '12 at 01:35 AM

DaveA gravatar image

DaveA
26.5k 151 171 256

Thanks for the reply Dave, still no luck. The first placed object with the tag is deleted but if I try and choose the second placed object first it remains no matter what.

Oct 12 '12 at 01:45 AM Konkor4
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x323
x291
x116
x103

asked: Oct 12 '12 at 01:33 AM

Seen: 258 times

Last Updated: Oct 12 '12 at 04:35 AM