So I fire a raycast, which acts as a gun, and when it hits a bird it is supposed to explode and destroy the bird gameobject. So, here is my problem, while it always spawns the explosion and plays the sound, it doesn’t always destroy the bird. And when it doesn’t it leaves the all dreaded object reference not set error. This has completely stumped me, any ideas why?
I also fall through the terrain sometimes when I shoot a bird, the explosion and sound still happens, but the terrain just deletes, Im not sure if this is related or if the bird deletes when I do it, (and the terrain has the “dirt” tag)
Thanks, and here is my script and a picture of the bird gameobject inspector settings.
[15231-screen+shot+2013-09-07+at+4.23.04+pm.png|15231]
[15230-screen+shot+2013-09-07+at+4.04.17+pm.png|15230]
var hit : RaycastHit;
var dirtPrefab : GameObject;
var birdexplode : GameObject;
var muzzlePoint : Transform;
var c1 : Color = Color.grey;
var c2 : Color = Color.white;
var bulletdistance = 200000;
var lineRenderer : LineRenderer;
function Start() {
lineRenderer = gameObject.AddComponent(LineRenderer);
lineRenderer.material = new Material (Shader.Find("Particles/Additive"));
lineRenderer.SetColors(c1, c2);
lineRenderer.SetWidth(0.04,0.01);
lineRenderer.SetVertexCount(2);
}
var timer : float;
var timeToWait : float = 0.1;
function Update(){
timer += Time.deltaTime;
if(timer > timeToWait)
{
var origin = transform.position;
var direction = transform.forward;
var endPoint = origin + direction * bulletdistance;
lineRenderer.SetPosition(0, origin);
if(Input.GetButtonDown("Fire1")){
StartCoroutine("Shoot");
//lineRenderer.SetVertexCount(2);
audio.Play();
}
}
}
function Shoot(){
var fwd = muzzlePoint.TransformDirection (Vector3.forward);
if (Physics.Raycast (muzzlePoint.position, fwd, hit, bulletdistance)){
timer = 0;
endPoint = hit.point;
lineRenderer.SetPosition(1, endPoint);
if(hit.collider.gameObject.tag == "dirt"){
Instantiate(dirtPrefab, hit.point, hit.transform.rotation);
// Invoke("LineRenderClear", 1);
}
if(hit.collider.gameObject.tag == "flyinganimal"){
Invoke("destroyhit", 1.3);
hit.transform.audio.Play();
Debug.Log("YOU HIT DA BIRD");
hit.transform.gameObject.GetComponent("Seagull").enabled = false;
Instantiate(birdexplode, hit.point, hit.transform.rotation);
}
}
}
function LineRenderClear () {
// lineRenderer.SetVertexCount(0);
}
function destroyhit () {
Destroy(hit.transform.gameObject);
}
Thanks Again,