- Home /
ArgumentOutOfRangeException when trying to create prefab torpedo shots (Bullets)
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[UnityEngine.GameObject].get_Item (Int32 index) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) Player.CreateTorpedo () (at Assets/Scripts/Player.cs:65)
I Used These Two Classes to make the torpedo shots(torpedos are the same mechanics as "bullets")
(Scroll the codes to the Right to see some long lines)
//--------------------------TORPEDO.CS---------------------------
using UnityEngine;
using System.Collections;
public class Torpedo : MonoBehaviour
{
public GameObject torpedo;
private float speed;
void Start ()
{
speed = 30f;
}
void Update ()
{
torpedo.transform.position += new Vector3 (speed, 0, 0) * Time.deltaTime;
if (torpedo.transform.position.x > Camera.main.ViewportToWorldPoint (new Vector3 (1, 0, 0)).x)
{
torpedo.SetActive (false);
}
}
}
//---------------------------PLAYER.CS---------------------------
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Player : MonoBehaviour
{
public List<GameObject> torpedoList;
//-----------------------
public GameObject player;
public GameObject prefab;
//-----------------------
private float speed;
private float interval;
private float timer;
//-----------------------
private bool flip;
void Start ()
{
player.transform.position = new Vector3 (0, 2.3f, -0.69f);
torpedoList = new List<GameObject>();
speed = .125f;
flip = false;
timer = 0f;
interval = .05f;
}
void Update ()
{
if (Input.GetKey (KeyCode.RightArrow) && player.transform.position.x < 6.25)
{
player.transform.position += new Vector3 (speed, 0, 0);
player.transform.eulerAngles = new Vector3 (0, 0, 0);
flip = false;
}
if (Input.GetKey (KeyCode.LeftArrow) && player.transform.position.x > -6.25)
{
player.transform.position -= new Vector3 (speed, 0, 0);
player.transform.eulerAngles = new Vector3 (0, 180, 0);
flip = true;
}
if (Input.GetKey (KeyCode.UpArrow) && player.transform.position.y < 2.432)
{
player.transform.position += new Vector3 (0, speed, 0);
}
if (Input.GetKey (KeyCode.DownArrow) && player.transform.position.y > -3.45)
{
player.transform.position -= new Vector3 (0, speed, 0);
}
if (Input.GetKey (KeyCode.S) && !flip)
{
timer+= Time.deltaTime;
if (timer > interval)
{
CreateTorpedo ();
timer = 0f;
}
}
}
private void CreateTorpedo ()
{
GameObject torpTemp = null;
for (int i = 0; i < 12; i++)
{
if (torpedoList[i].activeSelf == false)
{
torpTemp = torpedoList[i];
torpTemp.SetActive(true);
break;
}
}
if (torpTemp != null)
{
torpTemp.transform.position = new Vector3
(transform.position.x + 1.7f, transform.position.y + 1.5f, transform.position.z);
}
}
}
Answer by HarshadK · Jan 05, 2015 at 07:47 AM
In your Player.cs you are initializing your torpedoList but are not assigning any items to it anywhere. So you list is null and inside your for loop in CreateTorpedo() you are trying to access it hence ArgumentOutOfRangeException.
Your answer
![](https://koobas.hobune.stream/wayback/20220613175627im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to Fix Compilier Errors? 0 Answers
NullReferenceException: Object reference not set to an instance of an object. 2 Answers
Teleportation Problem 1 Answer
error cs0120 1 Answer