I am currently trying to make it so that when I click it creates RayCast to the position of the mouse and then destroy the object it hits, then play a death animation I have created. I keep getting error messages that I can’t figure out can anyone help me please?
Here is what I have currently…
#pragma strict
function Start ()
{
}
function Update ()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log(Camera.main.ScreenPointToRay(Input.mousePosition));
//make sure you have a camera in the scene tagged as 'MainCamer'
var hitm : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if(hitm.collider != null)
{
GameObject.Find("LeftSideSheeep").animation.Play("DeathAnimation");
Destroytimer ();
{
function Destroytimer (){
yield waitForSeconds (animation["DeathAnimation"].length);
Debug.Log ("Target Position: " + hitm.collider.gameObject.transform.position);
Destroy (hitm.collider.gameObject);
}
}
}
}
The errors I am getting are as follows,
(22,26) expecting ( , found Destroytimer
(22,41) ‘;’ expected
#pragma strict
function Destroytimer ()
{
yield WaitForSeconds (animation["DeathAnimation"].length);
}
function Update ()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log(Camera.main.ScreenPointToRay(Input.mousePosition));
//make sure you have a camera in the scene tagged as 'MainCamer'
var hitm : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if(hitm.collider != null)
{
GameObject.Find("MenuSheep_0").animation.Play("DeathAnimation");
Destroytimer ();
Debug.Log ("Target Position: " + hitm.collider.gameObject.transform.position);
Destroy (hitm.collider.gameObject);
}
}
}
But it doesn’t play the animation or destroy the sheep anymore. Also there is no errors until I actually try to destroy the sheep, and when I do I get this error “MissingComponentException: There is no ‘Animation’ attached to the “MenuSheep_0” game object, but a script is trying to access it”.
Thanks if you could help me out that would be awesome. If you could I won’t be monitoring my thread for a little while but if you have skype and could add me that would be really helpful. My Skype name is Nolivesleft. Sorry if I am asking too much.
Okay, First off you are trying to access the animation after you press your mouse button and the raycast returns a valid hit. GameObject.Find(“MenuSheep_0”).animation.Play(“DeathAnimation”);
apparently it cant find the DeathAnimation on MenuSheep_0. So you need to verify it’s there. Second verify that it actually found MenuSheep_0.
Are you certain the MenuSheep_0 Object is what hitm.collider struck if it was a different object that object might not have any animations attached to it. so you would be trying to destroy an object that shouldn’t have been destroyed.
What you can do is assign MenuSheep_0 a tag like enemy or even sheep. then when you can say
if(hitm.collider.tag == “sheep”)
{
hitm.collider.gameObject.animation.Play(“DeathAnimation”);
Destroytimer (); Debug.Log ("Target Position: " + hitm.collider.gameObject.transform.position); Destroy (hitm.collider.gameObject);
}
you can do it this way because you know for a fact the hitm.collider hit the Sheep and you know the sheep model has the animation on it.