- Home /
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".
is there a line number with that error message? possibly 51???
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();
}
}
}
}
Your answer
Follow this Question
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