I have this script which casts a ray when the mouse button is clicked
what i want to do it make it so what ever object the ray hits it returns the name of the object and destroys that object. for example im using it for a top down ish shooter game so the object it hits will be an enemy however there will be more then one of the same enemy in the scene at once so can you make it that what ever object the ray hits to destroy that singular ibect not every one of thoes object?
help would be awesome
you can use an out RaycastHit as one of the parameters for Physics.Raycast (look at its overloaded options) Then
answered Apr 22, 2011 at 03:29 PM
@ Bryan this is how im interpretation the code you gave me
except i get a null reference error Help?
answered Apr 23, 2011 at 02:10 AM
Well from what i read i hope is correct you have multiple enemies right and when the ray collides with the bad guy you want to destroy the game object. Now the easiest way that i can think of is creating variables at the top which carry gameObject in this case your enemies. every time your ray collides variable set gameObject with the enemy in it the ray will destroy the gameObject with the right tag. here is what i would do:
Now the way that this works is really simple if the ray hits the following enemy stored in the gameObject it will destroy it. don't forget to assign the game object in your inspector mode. If this does not work please let me know thanks :)
Problem Update :
i have solved your problem :) basically the problem was in the ray itself, this is because if you remember on the top of update i created a variable called var Hit: RayCastHit
this became a major part in your ray because every time it would shoot the ray out i was told to destroy an object that was stored in the hit variable. So all i need to do was add the following to the following line:
i have pasted the code that had the changes you will now notice that after diagonal i have placed "HIT" Now we have told the ray once you shoot out your diagonal bullet look for the stored object in the hit. And then at the bottom we have just stored the hit to enemy 1 and then destroy the object. now the second thing you may find different is the Destroy call instead of using the 3 var gameobjects at the top i just told it to destroy the object that is collided with the hit in this case an Enemy that has the tag "Enemy1"
hope it helps :)