- Home /
Question by
squary101 · Jul 03, 2015 at 10:12 PM ·
raycastnoobparsing error
I am trying destroy a game object using ray cast! Not working?
using UnityEngine; using System.Collections;
public class RemoveObstruction : MonoBehaviour { GameObject mainCamera;
// Use this for initialization
void Start ()
{
mainCamera = GameObject.FindWithTag("MainCamera");
}
// Update is called once per frame
void Update ()
{
if(Input.GetKeyDown (KeyCode.R)){
int x = Screen.width / 2;
int y = Screen.width / 2;
}
Ray ray = mainCamera.camera.ScreenPointToRay(new Vector3(x,y));
RaycastHit hit;
if(Physics.Raycast(ray,out hit)){
Obstruction o = hit.collider.GetComponent<Obstruction>;
Destroy (o);
}
}
}
Comment
Answer by thisisaunityaccount · Jul 04, 2015 at 07:50 AM
Try destroying the game object with the Obstruction component.
Destroy( o.gameObject );
Or just cut out the middle man and destroy the game object without getting its Obstruction component.
And, of course, make sure the object you're trying to destroy has a collider.
Your answer
Follow this Question
Related Questions
having problems with my raycast shooting script 0 Answers
Calling vector3/Sprite animation/Tutorials 0 Answers
Parsing Error at End of Script? 0 Answers
Raycast not working in 2D game 1 Answer