- Home /
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";
}
}
}
Answer by Noah-1 · Oct 12, 2012 at 02:35 AM
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 :)
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...
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...
I've just set this as my code:
[code] var PageCount : GUIText;
function Touched(hit:RaycastHit) { Destroy (gameObject); }
function Update () { if ( Input.Get$$anonymous$$ouseButtonDown(0) ) { var hit : RaycastHit; var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition); if (Physics.Raycast (ray, hit, 0.8)) { hit.transform.Send$$anonymous$$essage("Touched",hit, Send$$anonymous$$essageOptions.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.
Answer by DaveA · Oct 12, 2012 at 01:35 AM
if (Physics.Raycast (ray, hit, 0.8))
{
if (hit.transform.gameObject.tag == "Paper")
Destroy(hit.transform.gameObject);
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.