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 Vioswift · Jan 21, 2015 at 10:14 AM · array

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?

Comment
Add comment · Show 2
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 Vioswift · Jan 21, 2015 at 03:25 AM 0
Share

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

avatar image Vioswift · Jan 21, 2015 at 08:00 AM 0
Share

When I type

Debug.Log(weps.Length.ToString());

I get 0 as the length it should be 2

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 6 · 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
avatar image Vioswift · Jan 21, 2015 at 10:40 AM 0
Share

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;

 }
 
avatar image Vioswift · Jan 21, 2015 at 10:57 AM 0
Share

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

avatar image Mmmpies · Jan 21, 2015 at 11:03 AM 0
Share

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.

avatar image Vioswift · Jan 21, 2015 at 11:23 AM 0
Share

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);
         }
     }
 }

avatar image Mmmpies · Jan 21, 2015 at 11:28 AM 0
Share

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;
  }

Show more comments
avatar image
0

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;
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make an array list set to false gradually instead of instantly? 2 Answers

Multidimensional arrays - editing one element within one of the arrays 1 Answer

rotating vertices with for loop in 0 Answers

Create GUI based on an array 2 Answers

Instanced Prefabs: Audio clip array out of range 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