hello unity world
a noob here trying to script a tower defense game but I’ve encountered a problem. I’m scripting a SAM (surface to air) turret but the projectile is not firing. everything seems to work fine meaning the tower tilts, pans, aims, and follows the enemy but it doesn’t fire. you can hear the audio (meaning you can here the tower fire) but no projectile comes out.
i keep getting two errors
1. “indexOutOfRangeException: Array is out of range.
2. MissingRefenceException: The object type ‘GameObject’ has been destroyed but you are still trying to access it.
When i go to check the errors in the console both errors reference line 63 of the code below. I’ve marked it with an asterisk emit icon.
Any suggestions? Any help would be greatly appreciated!
#pragma strict
var myProjectile : GameObject;
var reloadTime : float = 1f;
var turnSpeed : float = 5f;
var firePauseTime : float = .25f;
var errorAmount : float = .001;
var myTarget : Transform;
var muzzlePositions : Transform[ ];
var pivot_Tilt : Transform;
var pivot_Pan : Transform;
var aim_Pan : Transform;
var aim_Tilt : Transform;
private var nextFireTime : float;
function Start ()
{
}
function Update ()
{
if(myTarget)
{
aim_Pan.LookAt(myTarget);
aim_Pan.eulerAngles = Vector3(0, aim_Pan.eulerAngles.y, 0);
aim_Tilt.LookAt(myTarget);
pivot_Pan.rotation = Quaternion.Lerp(pivot_Pan.rotation, aim_Pan.rotation, Time.deltaTimeturnSpeed);
pivot_Tilt.rotation = Quaternion.Lerp(pivot_Tilt.rotation, aim_Tilt.rotation, Time.deltaTimeturnSpeed);
if(Time.time >= nextFireTime)
{
FireProjectile();
}
}
}
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.tag == “Enemy”)
{
nextFireTime = Time.time+(reloadTime*.5);
myTarget = other.gameObject.transform;
}
}
function OnTriggerExit(other : Collider)
{
if(other.gameObject.transform == myTarget)
{
myTarget = null;
}
}
function FireProjectile()
{
audio.Play();
nextFireTime = Time.time+reloadTime;
var m : int = Random.Range(0,6);
:!: var newMissile = Instantiate(myProjectile, muzzlePositions[m].position, muzzlePositions[m].rotation); :!:
newMissile.GetComponent(Projectile_Missile).myTarget = myTarget;
}