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 Yamin-Nather · Nov 09, 2015 at 11:06 AM · arrayindexout of range

Out of range error

Im trying to make a InspectorWindow which works lik the array modifier. This is the code im using:

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 public class SpecialDuplicate : EditorWindow {
     public GameObject ToDuplicate;
     public Vector3 Pos;
     public Vector3 Rot;
     public Vector3 Scale;
     public float Count;
     private GameObject[] Duplicated = new GameObject[25];
     public GameObject Prefab;
     [MenuItem("Custom/Special Duplicate")]
     public static void CreatespecialDuplicateWindow () {
         EditorWindow.GetWindow (typeof(SpecialDuplicate));
     }
     
 
     void OnGUI () {
         GUILayout.Label ("Select GameObject");
         ToDuplicate = (GameObject)EditorGUILayout.ObjectField ("GameObject",ToDuplicate,typeof(GameObject));
         GUILayout.Label ("Transform Difference");
         Pos = EditorGUILayout.Vector3Field ("Position",Pos);
         Rot = EditorGUILayout.Vector3Field ("Rotation",Rot);
         Scale = EditorGUILayout.Vector3Field ("Scale",Scale);
         GUILayout.Label ("Number of Duplcations");
         Count = EditorGUILayout.FloatField ("Count",Count);
         if(GUILayout.Button ("Duplicate"))
         {
             Prefab = PrefabUtility.CreatePrefab("Assets/CustomModifiers/Array/DuplicatingPrefab.prefab",ToDuplicate,ReplacePrefabOptions.Default);
             for(int i = 0;i < Count;i++)
             {
 
                 Duplicated[i] = (GameObject)Instantiate(Prefab,Vector3.zero,Quaternion.identity);
                 if(i == 0)
                 {
                     Duplicated[i].transform.position = new Vector3(ToDuplicate.transform.position.x + Pos.x,ToDuplicate.transform.position.y + Pos.y,ToDuplicate.transform.position.z + Pos.z);  
                     Duplicated[i].transform.rotation = new Quaternion(ToDuplicate.transform.rotation.x + Rot.x,ToDuplicate.transform.rotation.y + Rot.y,ToDuplicate.transform.rotation.z + Rot.z,0f) ;
                     Duplicated[i].transform.localScale = new Vector3(ToDuplicate.transform.localScale.x + Scale.x,ToDuplicate.transform.localScale.y + Scale.y,ToDuplicate.transform.localScale.z + Scale.z) ;
 
                 }
                 else
                 {
                     Duplicated[i].transform.position = new Vector3(Duplicated[i - 1].transform.position.x + Pos.x,Duplicated[i - 1].transform.position.y + Pos.y,Duplicated[i - 1].transform.position.z + Pos.z);  
                     Duplicated[i].transform.rotation = new Quaternion(Duplicated[i - 1].transform.rotation.x + Rot.x,Duplicated[i - 1].transform.rotation.y + Rot.y,Duplicated[i - 1].transform.rotation.z + Rot.z,0f) ;
                     Duplicated[i].transform.localScale = new Vector3(Duplicated[i - 1].transform.localScale.x + Scale.x,Duplicated[i - 1].transform.localScale.y + Scale.y,Duplicated[i - 1].transform.localScale.z + Scale.z)  ;
                 }
                 Duplicated[i].transform.name = ToDuplicate.transform.name + (i + 1).ToString();
             }
             for(int i = 0;i < Count;i++)
             {
                 Duplicated[i] = null;
             }
         }
     }
 }

But when i try to access Duplicate[i] in the script it shows the error: "IndexOutOfRangeException:Array index is out of range".

Comment
Add comment · Show 1
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 gjf · Nov 07, 2015 at 11:15 PM 0
Share

is there a line number with that error message? possibly 51???

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Garazbolg · Nov 09, 2015 at 11:41 AM

I guess that the problem doesn't happen if you try to duplicate with less then 25 items, right ? The problem is that your 'Duplicated' array contains only 25 elements so if you try to acces the 26th item hit won't find anything.

One solution would be to reallocate your array each time you press the button like this :

 if(GUILayout.Button ("Duplicate"))
          {
                Duplicated = new GameObject[Mathf.Round(Count)];
 ...

But maybe the is a better way then even using that. I looked at the rest of the code and you would have run inton some other issues. Quaternions don't work like Vectors at all. So a way to simplify you code a lot and make it less ressources consuming :

 using UnityEngine;
  using System.Collections;
  using UnityEditor;
  public class SpecialDuplicate : EditorWindow {
      public GameObject ToDuplicate;
      public Vector3 Pos;
      public Vector3 Rot;
      public Vector3 Scale;
      public int Count;
      [MenuItem("Custom/Special Duplicate")]
      public static void CreatespecialDuplicateWindow () {
          EditorWindow.GetWindow (typeof(SpecialDuplicate));
      }
      
  
      void OnGUI () {
          GUILayout.Label ("Select GameObject");
          ToDuplicate = (GameObject)EditorGUILayout.ObjectField ("GameObject",ToDuplicate,typeof(GameObject));
          GUILayout.Label ("Transform Difference");
          Pos = EditorGUILayout.Vector3Field ("Position",Pos);
          Rot = EditorGUILayout.Vector3Field ("Rotation",Rot);
          Scale = EditorGUILayout.Vector3Field ("Scale",Scale);
          GUILayout.Label ("Number of Duplcations");
          Count = EditorGUILayout.IntField ("Count",Count);
          if(GUILayout.Button ("Duplicate"))
          {
             GameObject newInstance;
              Vector3 newPosition = ToDuplicate.transform.position;
              Quaternion newRotation = ToDuplicate.transform.rotation;
              Vector3 newScale = ToDuplicate.transform.localScale;
              for(int i = 0;i < Count;i++)
              {
               newPosition += Pos;
               newRotation *= Rot;
               newScale += Scale; // but i think you want to have '*=' instead of '+='
               newInstance =(GameObject)Instantiate(ToDuplicate,newPosition,newRotation);
               newInstance.tranform.localScale = newScale;
               newInstance.transform.name = ToDuplicate.transform.name + (i + 1).ToString();
               }
          }
      }
  }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I check a List[0], when it's nothing? 1 Answer

Array index is out of range Error, only when array is used to Instantiate 4 Answers

How to check if a c# 3d array index exists? 1 Answer

Array of transform index out of range error 1 Answer

IndexOutOfRangeException: Array index is out of range. 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