- Home /
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.
i am trying to run my game but everytime i get this exception.
My Script :
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class GameManager : MonoBehaviour { public List Characters = new List (); bool ShowCharWheel; public int SelectedCharacter; int LastCharacter; public static GameManager Instance;
void Awake()
{
Instance = this;
foreach(Character c in Characters)
{
c.Instance = Instantiate(c.PlayerPrefab,c.HomeSpawn.position, c.HomeSpawn.rotation) as GameObject;
}
ChangeCharacter (Characters [PlayerPrefs.GetInt("SelectedChar")]);
}
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.C)){
ShowCharWheel = true;
Screen.showCursor = true;
}
else{
ShowCharWheel = false;
Screen.showCursor = false;
}
}
void ChangeCharacter(Character c)
{
LastCharacter = SelectedCharacter;
SelectedCharacter = Characters.IndexOf (c);
Characters[LastCharacter].Instance.GetComponent<PlayerController>().CanPlay = false;
Characters[SelectedCharacter].Instance.GetComponent<PlayerController>().CanPlay = true;
Camera.main.GetComponent<SmoothFollow>().target = Characters[SelectedCharacter].Instance.transform;
PlayerPrefs.SetInt ("SelectedChar", SelectedCharacter);
}
void OnGUI()
{
if (ShowCharWheel)
{
GUILayout.BeginArea(new Rect(Screen.width - 64, Screen.height - 192,62,192),GUIContent.none,"box");
foreach(Character c in Characters)
{
if (GUILayout.Button(c.Icon,GUILayout.Width(64),GUILayout.Height (64)))
{
ChangeCharacter(c);
}
}
GUILayout.EndArea();
}
}
}
[System.Serializable] public class Character { public string Name; public Texture2D Icon; public GameObject PlayerPrefab; public GameObject Instance; public Transform HomeSpawn;
}
Comment
Your answer
Follow this Question
Related Questions
Identifier help 1 Answer
Call a Void on Collision 2 Answers
Have null errors 1 Answer