- Home /
Unity C# - Index out of array?
this is my code
[System.Serializable]
public class MultiDimensionalFloat
{
//public enum myEnum {Classic_Pistol, Classic_Assult_Rifle, Classic_Rocket_Launcher};
//public myEnum[] WepSlot;
public string wepName;
public GameObject WepGameobject;
public GameObject WhenHitGameobject;
public float coolDown;
public float ammo;
public float range;
public float damage;
public float weightKG;
public bool hasProjectile;
public GameObject projectileGameObject;
}
public MultiDimensionalFloat[] weps = new MultiDimensionalFloat[2];
// Use this for initialization
void Start () {
//adding weapons
weps[1].wepName = "Classic Pistol";
weps[1].WepGameobject = Instantiate(Resources.Load("myPrefab")) as GameObject;
weps[1].damage = 50;
weps[1].coolDown = 0.2f;
}
for some reason im getting a index out of range for the code in the Start class, this does not make sense! I put 2 free slots in this array, why doesn't it work? can someone please help me?
Yes I did do something like weps[2].wepName = "Classic Pistol"; In the other script. But I did not use the number 2 last time. but this method I used reduced it down to 1 error which is
IndexOutOfRangeException: Array index is out of range. Perform_Attack.Start () (at Assets/Scripts/Perform_Attack.cs:47)
which is weps[1].wepName = "Classic Pistol";
I'm not sure how it is out of index
When I type
Debug.Log(weps.Length.ToString());
I get 0 as the length it should be 2
Answer by Mmmpies · Jan 21, 2015 at 10:34 AM
Your script works fine from what you're showing however arrays start at 0 not 1 so if you have 2 elements in an array then they = 0 and 1 and not 1 and 2.
EDIT
Well I run your script but put the second part in another script so:
using UnityEngine;
using System.Collections;
public class TestScript : MonoBehaviour {
public MultiDimensionalFloat[] weps = new MultiDimensionalFloat[2];
public GameObject MyPrefab;
// Use this for initialization
void Start () {
//adding weapons
weps[1].wepName = "Classic Pistol";
weps[1].WepGameobject = Instantiate(MyPrefab) as GameObject;
weps[1].damage = 50;
weps[1].coolDown = 0.2f;
}
}
Get no errors at all with either 1 or 0 as the array element.
Yeah, I know that I was just trying different things to get it to work, I changed it back to 0, still get the same problem. I don't understand why i'm getting the error, am I missing something?
Its this now
[System.Serializable]
public class $$anonymous$$ultiDimensionalFloat
{
//public enum myEnum {Classic_Pistol, Classic_Assult_Rifle, Classic_Rocket_Launcher};
//public myEnum[] WepSlot;
public string wepName;
public GameObject WepGameobject;
public GameObject WhenHitGameobject;
public float coolDown;
public float ammo;
public float range;
public float damage;
public float weight$$anonymous$$G;
public bool hasProjectile;
public GameObject projectileGameObject;
}
public $$anonymous$$ultiDimensionalFloat[] weps = new $$anonymous$$ultiDimensionalFloat[3];
// Use this for initialization
void Start () {
Debug.Log(weps.Length.ToString());
//adding weapons
weps[0].wepName = "Classic Pistol";
weps[0].WepGameobject = Instantiate(Resources.Load("myPrefab")) as GameObject;
weps[0].damage = 50;
weps[0].coolDown = 0.2f;
}
So I tried tried your way and it doesn't know what
weps[1].wepName = "Classic Pistol";
weps[1].WepGameobject = Instantiate($$anonymous$$yPrefab) as GameObject;
weps[1].damage = 50;
weps[1].coolDown = 0.2f;
is
Well I put the prefab in as a public GameObject just for speed, leave your Resouces.Load. Just make sure this
using UnityEngine;
[System.Serializable]
public class $$anonymous$$ultiDimensionalFloat
{
public string wepName;
public GameObject WepGameobject;
public GameObject WhenHitGameobject;
public float coolDown;
public float ammo;
public float range;
public float damage;
public float weight$$anonymous$$G;
public bool hasProjectile;
public GameObject projectileGameObject;
}
is in one script and..
public $$anonymous$$ultiDimensionalFloat[] weps = new $$anonymous$$ultiDimensionalFloat[3];
// Use this for initialization
void Start () {
Debug.Log(weps.Length.ToString());
//adding weapons
weps[0].wepName = "Classic Pistol";
weps[0].WepGameobject = Instantiate(Resources.Load("myPrefab")) as GameObject;
weps[0].damage = 50;
weps[0].coolDown = 0.2f;
is in another one.
your method works, but not fully I can't get passed 2
using UnityEngine;
using System.Collections;
public class Weapons : $$anonymous$$onoBehaviour {
[System.Serializable]
public class $$anonymous$$ultiDimensionalFloat
{
//public enum myEnum {Classic_Pistol, Classic_Assult_Rifle, Classic_Rocket_Launcher};
//public myEnum[] WepSlot;
public string wepName;
public GameObject WepGameobject;
public GameObject WhenHitGameobject;
public float coolDown;
public float ammo;
public float range;
public float damage;
public float weight$$anonymous$$G;
public bool hasProjectile;
public GameObject projectileGameObject;
}
public $$anonymous$$ultiDimensionalFloat[] weps = new $$anonymous$$ultiDimensionalFloat[6];
// Use this for initialization
void Start () {
//adding weapons
weps[0].wepName = "Classic Pistol";
//weps[0].WepGameobject = Instantiate(Resources.Load("myPrefab")) as GameObject;
weps[0].damage = 50;
weps[0].coolDown = 0.2f;
weps[0].ammo = 100;
weps[0].range =100.0f;
weps[1].wepName = "Classic Assult Rifle";
weps[2].wepName = "$$anonymous$$nife";
//weps[3].wepName = "Classic Rocket Launcher";
//weps[4].wepName = "Hands";
for(int i = 0; i < weps.Length; i++)
{
Debug.Log(weps[i].wepName);
}
}
}
Well you've set it to be 6 in length so you should be able to. What error are you getting?
EDIT Although it looks like you've put your class back in the same script. Don't do that, keep the class that doesn't inherit from monoBehaviour in a separate script as I said:
using UnityEngine;
[System.Serializable]
public class $$anonymous$$ultiDimensionalFloat
{
public string wepName;
public GameObject WepGameobject;
public GameObject WhenHitGameobject;
public float coolDown;
public float ammo;
public float range;
public float damage;
public float weight$$anonymous$$G;
public bool hasProjectile;
public GameObject projectileGameObject;
}
Answer by CHPedersen · Jan 21, 2015 at 11:03 AM
The array is public, which means that Unity is serializing it (it appears in the editor). Maybe there's a version of that variable serialized in Unity as having a length of 0, and this initialization is overwriting your attempt to instantiate it to an array of length 2.
I suggest you try to disable the serialization of this array by making it private, then rerun this code.
If you need another script to access the array, you can expose it through a public property instead, see example:
public MultiDimensionalFloat[] Weps { get { return weps; } }
private MultiDimensionalFloat[] weps = new MultiDimensionalFloat[2];
// Use this for initialization
void Start()
{
Debug.Log(weps.Length.ToString());
//adding weapons
weps[0].wepName = "Classic Pistol";
weps[0].WepGameobject = Instantiate(Resources.Load("myPrefab")) as GameObject;
weps[0].damage = 50;
weps[0].coolDown = 0.2f;
}
Your answer