Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

tower defense scripting problem

Discussion in 'Scripting' started by lrobles99, Jul 6, 2013.

  1. lrobles99

    lrobles99

    Joined:
    Jul 6, 2013
    Posts:
    34
    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.deltaTime*turnSpeed);
    pivot_Tilt.rotation = Quaternion.Lerp(pivot_Tilt.rotation, aim_Tilt.rotation, Time.deltaTime*turnSpeed);

    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;
    }
     
  2. YodelYum

    YodelYum

    Joined:
    Apr 3, 2013
    Posts:
    44
    Well indexOutOfRangeException: Array is out of range means that you try to access an index of an array that just isn't set. Meaning that if you had an array with 3 entries (indexes 0-2) and you tried to adress the 4entry (index 3) you would get an error, because it's simply not there.

    Could you explain what exactly muzzlePositions is and why it is an array? That would help understanding your problem.
     
  3. lrobles99

    lrobles99

    Joined:
    Jul 6, 2013
    Posts:
    34

    hmmm....your response seems to make things a little clear for me. i'm beginning to think i dont need the array at all. i took the script from another file that had a tower with 6 missile launchers (muzzlePositions), my tower only has one missile launcher. SHould i just keep the array and change the values to 0,0?

    how would i write that in code form?
     
  4. YodelYum

    YodelYum

    Joined:
    Apr 3, 2013
    Posts:
    44
    Yeah in that case I guess u wont need the array at all.
    When instantiating the missile I'd say, you just give it the position off where the projectile should launch from.