Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FGPArthurVII · Jan 05, 2015 at 11:01 AM · errorprefabsbulletsargumentexception

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);
         }
     }
 }
 
  
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges