I have a 2d project, I’m writing a script to cast a ray into colliders… I want to delete the collider I click on, but even though I’m probably staring the answer straight in the face in the documentation I can’t figure out how to get the Gameobject I am clicking on. I am able to get the transform, but I can’t use Destroy on the transform component. I need the whole game object. please help
//Player Script
//Inspector Variables
var EditableTagName : String;
var RaycastLength : float;
var GOtoBeDestroyed : GameObject;
var BlTriPLUS : GameObject;
var BlTriMINUS : GameObject;
var hit : RaycastHit;
var ray : Ray;
var sPlayer1Score : String;
var sPlayer2Score : String;
var fPlayer1ScoreNumber : float;
var fPlayer2ScoreNumber : float;
var fMoveRectangleDownYP1 : float = 0;
var fMoveRectangleDownYP2 : float = 0;
//***************SCOREBOARD***************************
function OnGUI () {
GUI.Label (Rect (100, 100, 1000, 200), "ScoreBoard");
sPlayer1Score = "Player One's Score is: " + fPlayer1ScoreNumber;
sPlayer2Score = "Player Two's Score is: " + fPlayer2ScoreNumber;
GUI.Label (Rect (130, fMoveRectangleDownYP1, 200, 200), sPlayer1Score);
GUI.Label (Rect (130, fMoveRectangleDownYP2, 200, 200), sPlayer2Score);
}
function Update ()
{
//Use the mouse button to select GameObjects during gameplay
//****************************************************************************
if(Input.GetMouseButtonDown(0))
{
print("Coder Message: Left Mouse Button Has Been Pressed");
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray,hit, RaycastLength))
{
if (hit.transform.tag == EditableTagName)
{
//gameObject.hit.transform.tag
BlTriPLUS.transform.position = hit.transform.position;
//Destroy(hit.GameObject);
Destroy(hit.GameObject);
Instantiate(BlTriPLUS);
print ("An object has been hit with a ray");
}
}
else
{
print (ray);
print ("you missed");
}
}
//****************************************************************************
if(Input.GetMouseButtonDown(1))
{
print("Coder Message: Right Mouse Button Has Been Pressed");
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray,hit, RaycastLength)) //if ray is cast
{
if (hit.transform.tag == EditableTagName) // if the ray hits something tagged
{
BlTriMINUS.transform.position = hit.transform.position;
Destroy(hit.transform);
Instantiate(BlTriMINUS);
print ("An object has been hit with a ray");
}
}
else
{
print (ray);
print ("you missed");
}
}
}