0

In this case animators contains 3 items. But let' say I don't know how many items there is. Could be 3 or 30. I can get the "medea_m_arrebola" index fine but how do I get the rest indexs ?

I tried on this line also 2 and now 1 : But getting exception IndexOutOfRangeException: Index was outside the bounds of the array.

soldiers_indexs = new int[1];

And if not using this line I'm getting null on the line :

soldiers_indexs[i] = i;

How can I get the rest items indexs ?

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class OpenningScene : MonoBehaviour
{
    [Header("Animators")]
    public Animator[] animators;
    [Space(5)]
    [Header("Movement Settings")]
    public Transform target;
    public float movingSpeed = 1f;
    public bool slowDown = false;
    [Space(5)]
    [Header("Rotation Settings")]
    public float rotationSpeed;
    public DepthOfField dephOfField;
    public float waitingAnimation;
    public float startConversation;

    private Vector3 targetCenter;
    private bool startWaitingAnim = true;
    private bool endRot = false;
    private int medea_m_arrebola_index;
    private int[] soldiers_indexs;

    // Use this for initialization
    void Start()
    {
        targetCenter = target.GetComponent<Renderer>().bounds.center;
        soldiers_indexs = new int[1];
        for (int i = 0; i < animators.Length; i++)
        {
            animators[i].SetFloat("Walking Speed", movingSpeed);

            if(animators[i].name == "medea_m_arrebola")
            {
                medea_m_arrebola_index = i;
            }
            else
            {
                soldiers_indexs[i] = i;
            }
        }
    }
Daniel Lip
  • 3,867
  • 7
  • 58
  • 120
  • 3
    If you declare an array having one element (as your code does), the only valid index to that array in most languages, including C#, is `0`. You definitely can't access it in a loop that goes for more than one pass, because it's a single element array. What would you expect to happen on the second pass of the loop when `i` is greater than zero? What exactly would `soldiers_indexs[1]` access, or `soldiers_indexs[2]` when you've declared `soldiers_indexs = new int[1];`? – Ken White Nov 16 '19 at 01:21
  • Use a list if you don't know the size of the array (and often even if you do - arrays are not used nearly as much anymore it seems): `private List soldiers_indexs;`, and then `soldiers_indexs = new List();`, and then `soldiers_indexs.Add(i);` – Rufus L Nov 16 '19 at 01:35
  • Also, take a look at the [Naming Guidelines](https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/naming-guidelines) for some best practices on naming in C#. Fields should be `PascalCase`, so it would be: `private List SoldierIndexes;` – Rufus L Nov 16 '19 at 01:39

1 Answers1

0

Because C# is not Javascript.

Your code with soldiers_indexs[i] = i; would work perfectly in Javascript, even if the end result is hard to understand. However, an array in C-type languages is a defined data structure with lower and upper bounds. C# does not allow you to do things that appear to make no sense.

As it's not clear from your code what you are attempting to do, I can't advise a forward path. However, it is clear that using the index variable i to refer to a place on an array that you are not iterating is a mistake.

Generally, in C#, we use more general-purpose data structures such as a List<T>. In addition, I do not recommend for loops. A foreach loop nearly always makes more sense, and is always easier to read.

theMayer
  • 15,456
  • 7
  • 58
  • 90