Hi, I’m really stock with my code. What have I done wrong? The game runs, I don’t know what cause of this error:
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
Even though I have made check if this arrayVar[index] is null (or did not exist/out of scope?) then don’t register this to tempVar… I’m using List… Is there something to do with foreach, StartCoroutine, List, or array*?*
Please help me what’s wrong with the script, thanks for your time…
Here’s what in my console:
I have Debug.Log (“Destination B-3: Went here successfully!”); Destination: B-3, B-4, B5 did not display in the same IEnumerator startRespawn. It did not reach there… That means my Instantiate did not happen…
I also check when that error appeared; it appeared after line 194 that is “if (bulletRush.timeCode >= thisTime.start && thisTime.duration > 0f)”. Thank you for your help.
Here’s my unfinished script: (You may copy and paste this for clarity)
```csharp
*using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class BulletModification {
[Header(“Modify Cluster:”)]
public float startSpeed;
public enum generateAcceleration {
byUnit,
byPercentage
} public generateAcceleration accelerationType;
public float acceleration;
public float speedRange;
}
[System.Serializable]
public class TransformFormula {
//[Header(“Stir Bullet:”)]
[Header(“Mirror Bullet:”)]
[Tooltip(“0 means all copies will be on sync, beyond that will make delay offset speed; 100 makes the last delay copy to stationary, 200 makes go to opposite direction. Set delayAmount above 0 to see result”)]
[Range(0,200)] public float speedOffset;
[Tooltip(“Number of clone bullets for offset speed. Set speedOffset to work with”)]
[Range(0,3)] public int delayAmount;
[Tooltip(“Number of clone bullets shoot parallel to each other”)]
[Range(1,128)] public int linearAmount = 1;
public enum spaceType {
byUnitAway,
byOverallWidth
}; [Tooltip(“How parallel bullets take up space”)] public spaceType areaOccupy;
[Tooltip(“The amount of parallel bullets away each other or their general width as one group”)]
public float spacingValue;
[Tooltip(“Number in seconds; parallel from the middle will start immediately, then delay start to the next neighbor line”)]
[Range(0,20)] public float delayLinear;
[Header("Spray Bullet:")]
[Tooltip("The degree angle of an arc be made for rayAmount")]
[Range(0,360)] public float spreadAngle;
[Tooltip("The number of copy bullet as rays restricted by spreadAngle")]
[Range(1,32)] public int rayAmount = 1;
[Tooltip("The radius offset of the center of the ray")]
public float spawnOffsetRadius;
[Tooltip("Number in seconds; the first ray will start immediately, then delay start to the next neighbor ray")]
[Range(0,20)] public float delayRay;
}
[System.Serializable]
public class TransformPattern {//bulletMotion
[Header(“Bullet Patterns:”)]
public Vector2 respawnOffset;
[Tooltip(“Sliding the respawn origin of bullet sideward from left to right length-based over given duration”)]
public float linearMagnitude;
public enum linearMotionType{
Linear,
Wave
}; public linearMotionType linearFlow;
public enum linearDirection{
left_Right,
right_Left,
center_Right,
center_Left
}; public linearDirection linearPoint;
[Tooltip(“Move with one direction or play ping pong”)]
public bool pingPong = true;
[Tooltip(“The spin progress over given duration; Note: clinch with ‘Linear Magnitude’”)]
public float spinMotion;
public enum spinMotionType {
Linear,
Wave
}; public spinMotionType spinFlow;
public enum spinDirection{
left_Right,
right_Left,
center_Right,
center_Left
}; public spinDirection spinPoint;
[Tooltip(“Merry-go-around or not”)]
public bool swingBack = false;
[Tooltip(“As it says; offset vertically and/or horizontally -_-”)]
public Vector2 masterOffset;
[Tooltip(“Offset rotation, but only in z-axis, any problem with that?! Note: doesn’t spin over time, use bullet formula instead or use emitter and make it spin via other script (or animation?)”)]
public float masterRotation;
}
[System.Serializable]
public class TimeLine {
[Header(“Bullet Time:”)]
[Tooltip(“Time in seconds to start the bullet party”)]
public float start;
[Tooltip(“Time in seconds to finish one pattern”)]
public float duration;
[Tooltip(“One more duration to repeat the pattern”)]
public int repeat;
[Tooltip(“Number of bullet emits in one span of duration harmonizing with pattern; Note: minimum fire rate will not less than 0.1 sec, or else actionPulse will be deducted”)]
public int actionPulse;
[Header("Bullet Element:")]
[Tooltip("To certainly choose none, input less than 0")]
public int pattern;
[Tooltip("To certainly choose none, input less than 0")]
public int formula;
[Tooltip("To certainly choose none, input less than 0")]
public int modification;
}
[System.Serializable]
public class BulletSpawn {
[Tooltip(“Your title, definition, description, speech, message, or whatever; :Optional:”)]
public string name;
public GameObject bulletPrefab;
[Tooltip(“Spawn point: object attached with this script act as default emitter, if leave empty”)]
public List emitter;
public List bulletTime;
}
[System.Serializable]
public class Bulletflow {
/* Note: for the future viewer; why no ‘public bool active’?
* No need, I found it useless; use/code timeHalt + reset = inactive (if you want to extend this code)… */
public bool reset;
public bool timeHalt;
public bool looping;
[Tooltip(“Set loopQuantity to zero for infinite loop”)]
public int loopQuantity;
[Tooltip(“What for? Delay the start time of timeCode”)]
public float delayStart;
[Tooltip(“Behold the time holder! NOLI ME TANGERE, if you don’t know me what I am for”)]
public float timeCode;
}
public class HellBulletManager : MonoBehaviour {
public Transform test;
[Header("Bullet Manager:")]
public Bulletflow bulletRush;
public List<BulletSpawn> hellBullets;
[Header("Bullet Transform:")]
public List<TransformPattern> bulletPattern;
[Header("Bullet Properties:")]
public List<TransformFormula> bulletFormula;
public List<BulletModification> bulletModification;
// Use this for initialization
void Start () {
if (bulletFormula.Count > 0)
foreach (TransformFormula thisFormula in bulletFormula)
{
//using [Range(min,max)] and min set to 1, but the default stay to 0; this is use to fix the min when game starts
if (thisFormula.linearAmount == 0)
thisFormula.linearAmount = 1;
if (thisFormula.rayAmount == 0)
thisFormula.rayAmount = 1;
}
}
void Update () {
//Debug.Log((hellBullets[0].bulletPrefab[0]));
if (bulletRush.reset)
{
bulletRush.reset = !bulletRush.reset;
bulletRush.timeCode = 0f;
}
if (bulletRush.timeHalt)
{
return;
}
if (bulletRush.delayStart > 0f)
{
bulletRush.delayStart -= Time.deltaTime;
if (bulletRush.delayStart < 0f)
bulletRush.delayStart = 0f;
}
else
{
bulletRush.timeCode += Time.deltaTime;
}
//Debug.Log((hellBullets[0].bulletPrefab));
//Make sure taken out all List with null bulletSpawn.bulletPrefab --- Lambda syntax 'COOL AND POWERFUL' they say!
hellBullets.RemoveAll (BulletSpawn => BulletSpawn.bulletPrefab == null);
if (hellBullets.Count > 0)
{
foreach (BulletSpawn thisBullet in hellBullets)
{
if (thisBullet.bulletTime.Count > 0) {
foreach (TimeLine thisTime in thisBullet.bulletTime)
{
//Debug.Log ("Watching here! No Error Appear...");
//Debug.Break ();
if (bulletRush.timeCode >= thisTime.start && thisTime.duration > 0f)
{
//Debug.Log ("Gotcha! Error Appeared here!"); //Why? Is foreach something to do with this? // Unity 5.4.1f1 (64-bit)
//Error: ArgumentOutOfRangeException: Argument is out of range. Parameter name: index
//Debug.Break ();
float pulseGap = thisTime.duration / thisTime.actionPulse;
if (pulseGap < 0.1f) {
thisTime.actionPulse = Mathf.FloorToInt(thisTime.duration / 0.1f);
if (thisTime.actionPulse == 0)
return;
pulseGap = thisTime.duration / thisTime.actionPulse;
}
Debug.Log ("Destination A-1: Went here successfully!");
StartCoroutine (startRespawn (thisBullet.bulletPrefab, thisBullet.emitter, pulseGap, thisTime.repeat, thisTime.pattern, thisTime.formula, thisTime.modification));
Debug.Log ("Destination A-2: Went here successfully!");
}
}
}
}
}
}
/*
void FixedUpdate ()
{
if (bulletRush.timeHalt)
{
//return;
}
//code here..
}
*/
IEnumerator startRespawn (GameObject bullet, List<GameObject> emitters, float fireRate, int repeat, int pattern, int formula, int modification)
{
TransformPattern patternApply;
TransformFormula formulaApply;
BulletModification modApply;
bool patternExist = false;
Vector3 respawnOffset;
Vector3 masterOffset;
Quaternion masterRotation;
bool formulaExist = false;
bool modificationExist = false;
Debug.Log ("Destination B-1: Went here successfully!");
//The game will run but gives me a message error, I don't know where cause of this specifically, may here or above there? Error: ArgumentOutOfRangeException: Argument is out of range. Parameter name: index
if (bulletPattern[pattern] != null) { //Checking... if (bulletPattern[pattern] == null) never assigned 'patternApply = bulletPattern [pattern];'...
patternExist = true;
patternApply = bulletPattern [pattern];
respawnOffset = new Vector2 (Random.Range (-patternApply.respawnOffset.x, patternApply.respawnOffset.x), Random.Range (-patternApply.respawnOffset.y, patternApply.respawnOffset.y));
masterOffset = transform.position + new Vector3 (patternApply.masterOffset.x, patternApply.masterOffset.y);
masterRotation = transform.rotation * Quaternion.Euler (0f, 0f, patternApply.masterRotation);
} else {
respawnOffset = Vector3.zero;
masterOffset = Vector3.zero;
masterRotation = Quaternion.identity;
//patternApply = null;
}
Debug.Log ("Destination B-2: Went here successfully!");
if (bulletFormula [formula] != null) { //if (bulletFormula.Exists(TransformFormula => bulletFormula[formula])) /* <- Sample only: trying '.Exists' for alternatives, but give me some errors; I don't know really how to use it... or how about some '.Contain'? I don't know about it, please help me... THANKS */
formulaExist = true;
formulaApply = bulletFormula [formula];
}
Debug.Log ("Destination B-3: Went here successfully!");
if (bulletModification[modification] != null) { //if (bulletModification.Exists(bulletModification[modification]))
modificationExist = true;
modApply = bulletModification [modification];
}
//Transform bulletDirector;
Debug.Log ("Destination B-4: Went here successfully!");
if (emitters != null || emitters.Count == 0)
Instantiate (bullet, transform.position + respawnOffset + masterOffset, transform.rotation * masterRotation);
/*else
foreach (Transform emitter in emitters)
{
}*/
Debug.Log ("Destination B-5: Went here successfully!");
yield return null;
}
}*
```
Thank you, your help is much appreciated…