Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 XD_Me_DX · Nov 08, 2016 at 07:36 AM · listfloatreplace

ArgumentOutOfRangeException: Argument is out of range. Parameter name: index What am I doing wrong?

When I move in the game, I get "ArgumentOutOfRangeException: Argument is out of range. Parameter name: index" pointing towards the "Playerpos[0] = fx;" Line! I understand an easy fix would be to make Playerpos a vector2, but because I am serializing Playerpos, it can't be a Vector2, so I believe this is the only way. What I am wanting is Playerpos to look like a vector2 (x,y) without it looking like a record of the player moves.

Basically, I am asking how to replace an element (Playerpos[0]) with an x/y coordinate?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class PlayerMovement : MonoBehaviour {
     [HideInInspector]
     public List<float> Playerpos;
     private float x;
     private float y;
     private float fx;
     private float fy;
     private float speed;
     private GameObject c;
     private float cz;
     [HideInInspector]
     public Vector2 playerpos;
     void Start(){
         c = GameObject.Find("Main Camera");
         cz = c.transform.position.z;
         x = this.transform.position.x;
         y = this.transform.position.y;}       
 
     void Update () {
         if (Input.GetButtonDown("Horizontal") && Input.GetAxisRaw("Horizontal") > 0 && Physics.CheckSphere(new Vector2((x+1),y), 0)){
             print("right");
             fx += 1;
             transform.position = new Vector3(fx, y, -2);
             c.transform.position = new Vector3(fx, y,cz);
             Playerpos[0]+= fx;
             x = fx;}
         else if (Input.GetButtonDown("Horizontal") && Input.GetAxisRaw("Horizontal") < 0 && Physics.CheckSphere(new Vector2((x-1), y), 0)){
             print("left");
             fx -= 1;
             transform.position = new Vector3(fx, y, -2);
             c.transform.position = new Vector3(fx, y, cz);
             Playerpos[0] = fx;
             print(Playerpos[1]);
             x = fx;}
         else if (Input.GetButtonDown("Vertical") && Input.GetAxisRaw("Vertical") > 0 && Physics.CheckSphere(new Vector2(x, (y+1)), 0)){
             print("up");
             fy += 1;
             transform.position = new Vector3(x, fy, -2);
             c.transform.position = new Vector3(x, fy, cz);
             Playerpos.Insert(1, fy);
             print(Playerpos);
             y = fy;}
         else if (Input.GetButtonDown("Vertical") && Input.GetAxisRaw("Vertical") < 0 && Physics.CheckSphere(new Vector2(x, (y-1)), 0)){
             print("down");
             fy -= 1;
             transform.position = new Vector3(x, fy, -2);
             c.transform.position = new Vector3(x, fy, cz);
             Playerpos.Insert(1, fy);
             y = fy;
             print(Playerpos);
         }
         playerpos = new Vector2(x, y);
     }
 }
 

This is my first time using Unity Answers, so apologies if I am being dumb, and Thank you for the future help!

Comment
Add comment · Show 3
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
avatar image Imankit · Nov 08, 2016 at 10:08 AM 3
Share

It clearly states that your PlayerPos list is empty and you have not initiaized the list as well. 1) Initialize the list with PlayerPos = new List(); 2) Check before accessing PlayerPos[0] that your List's Count > 0

avatar image XD_Me_DX Imankit · Nov 08, 2016 at 05:02 PM 0
Share

Thank you so much! While Playerpos = new List(2) didn't work, You put me in the right direction!

avatar image TreyH XD_Me_DX · Nov 08, 2016 at 06:48 PM 0
Share

Then upvote their suggestion ...

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

HOW TO ADD AND REMOVE VARIABLES ALREADY DECLARED IN A LIST 1 Answer

How to find the index of the min value in a list 1 Answer

floats and lists 1 Answer

How to set value from list to objects 1 by 1 0 Answers


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