Arrays in Instantiate?

Hi, I want to instantiate sounds from an array; however, whenever I do it does not work. It works if I don’t use the array. I also get this warning:

Assets/Scripts/GunScript.js(100,63): BCW0028: WARNING: Implicit downcast from 'Object' to 'UnityEngine.Object'.

This is my code:

var recoilX: float = 10;
var recoilY: float = 10;
var rotationSpeed: float = 0.03;
var holdHeight: float = -0.23;
var holdSide: float = 0.27;
var holdDistance: float = 0.52;
var cameraObject: GameObject;
var ratioHipToHold: float = 1;
var hipToAimSpeed: float = 0.1;
@HideInInspector
var ratioHipToHoldV: float;
@HideInInspector
var rotationTargetX: float;
@HideInInspector
var rotationTargetY: float;
@HideInInspector
var rotationTargetXVelocity: float;
@HideInInspector
var rotationTargetYVelocity: float;
var walkScript: PlayerMovementScript;
var aimingSensitivityGS: float = 0.25;
var aimingWalkSpeedGS: float = 0.5;
var aimFOV: float = 1;
@HideInInspector
var aimFOVV: float;
@HideInInspector
var currentFOV: float = 60;
var defaultFOV:float = 60;
@HideInInspector
var positionTargetVelocity: Vector3;
var positionMoveSpeed: float = .1;
var rateOfFire: float = 18;
@HideInInspector
var fireDelay: float = 0;
var bullet: GameObject;
var bulletSpawn: GameObject;
var ammo: int = 15;
var soundArrayPosition: float; 

//All the sounds
var sound1: GameObject;
var sound2: GameObject;
var sound3: GameObject;
var sound4: GameObject;
var sound5: GameObject;
var sound6: GameObject;
var sound7: GameObject;
var sound8: GameObject;
var sound9: GameObject;
var sound10: GameObject;
var sound11: GameObject;
var sound12: GameObject;
var sound13: GameObject;
var sound14: GameObject;
var sound15: GameObject;
var sound16: GameObject;
var sound17: GameObject;
var sound18: GameObject;
var sound19: GameObject;
var sound20: GameObject;


//Array of sounds
var soundArray = new Array(sound1, sound2, sound3, sound4, sound5, sound6, sound7, sound8, sound9, sound10, sound11, sound12, sound13, sound14, sound15, sound16, sound17, sound18, sound19, sound20);


function Start () {

}

function Update () 
{

	var soundContainer: GameObject;
	
	//Sets the sound array position
	soundArrayPosition = Mathf.Round(Random.Range(1,20));
	
	
	
	//sets the current FOV
	currentFOV = camera.main.fieldOfView;
	
	//Spawns the bullet, adds recoil, and sound
	if(Input.GetButtonDown("Fire1"))
	{
		if(fireDelay <= 0 && ammo > 0)
		{
			if (bullet)
			{
				Instantiate(bullet,bulletSpawn.transform.position, bulletSpawn.transform.rotation);
				cameraObject.GetComponent(MouseLookScript).rotationX -= (Random.value * recoilX);
				cameraObject.GetComponent(MouseLookScript).rotationY += ((Random.value - 0.5) * recoilY);
				
				if(soundContainer)
				{
					Instantiate(soundArray[soundArrayPosition], bulletSpawn.transform.position, bulletSpawn.transform.rotation);
				}
				fireDelay = 1;  
				ammo -= 1;
			}
		}
	}
	
	if(soundContainer)
	{
		soundContainer.transform.parent = transform;
	}
	
	//Reloads the weapon
	if(Input.GetButtonDown("Reload"))
	{
		ammo = 15;
	}
	
	fireDelay -= Time.deltaTime * rateOfFire;
	
	//Enables/disables iron sights. Also changes look sensitivity and walk speed
	if(Input.GetButton("Fire2"))
	{
		ratioHipToHold = Mathf.SmoothDamp(ratioHipToHold, 0, ratioHipToHoldV, hipToAimSpeed);
		cameraObject.GetComponent(MouseLookScript).aimingSensitivity = aimingSensitivityGS;
		walkScript.aimingWalkSpeed = aimingWalkSpeedGS;
		camera.main.fieldOfView = Mathf.SmoothDamp(currentFOV, aimFOV, aimFOVV, hipToAimSpeed);
	}
	else
	{
		ratioHipToHold = Mathf.SmoothDamp(ratioHipToHold, 1, ratioHipToHoldV, hipToAimSpeed);
		cameraObject.GetComponent(MouseLookScript).aimingSensitivity = 1;
		walkScript.aimingWalkSpeed = 1;
		if(currentFOV != defaultFOV)
		{
		camera.main.fieldOfView = Mathf.SmoothDamp(currentFOV, defaultFOV, aimFOVV, hipToAimSpeed);
		}
	}
	
	//Sets position
	transform.position = cameraObject.transform.position + (Quaternion.Euler(rotationTargetX,rotationTargetY,0) * Vector3(holdSide * ratioHipToHold, holdHeight *ratioHipToHold, holdDistance));
	
	//Rotates the gun with the camera
	rotationTargetX = Mathf.SmoothDamp(rotationTargetX, cameraObject.GetComponent(MouseLookScript).rotationX, rotationTargetXVelocity, rotationSpeed);
	rotationTargetY = Mathf.SmoothDamp(rotationTargetY, cameraObject.GetComponent(MouseLookScript).rotationY, rotationTargetYVelocity, rotationSpeed);
	transform.rotation = Quaternion.Euler(rotationTargetX * -1, rotationTargetY - 180, 0);
}

By the way, did I make the array correctly?

That’s the wrong way of doing arrays…instead of a million separate variables

var sound1: GameObject;
var sound2: GameObject;
// etc.

Just do:

var sounds : GameObject[];

Never use the Array class; it’s slow and obsolete. Always use built-in arrays, or generic List if you need a dynamically-sized array.

Also this isn’t right:

 soundArrayPosition = Mathf.Round(Random.Range(1,20));

When used with integers, Random.Range already returns an integer, so you don’t need to round anything. The upper bound is non-inclusive, and shouldn’t be hard-coded. Also array indices start with 0, not 1.

 soundArrayPosition = Random.Range(0, sounds.Length);

It should also be with the code that instantiates the object, instead of running every frame for no reason.

However you should really be using PlayClipAtPoint, since the current method you’re trying to use will spawn an endless number of GameObjects that are never cleaned up, so eventually it will crash.