-1

Some times my code runs but then without changing any of the code it stops working and says I have the error argument is out of range exception? it says it is on the line, "if (gameObject.transform.localPosition.y == finP[1].transform.localPosition.y)".

here is my code:

    public class enemy : MonoBehaviour
    {
        List<GameObject> finP = new List<GameObject>();
        public Node nodeScript;
        Vector2 direction = Vector2.zero;
        bool trigger = false;
        public float Speed = 1.0f;
        // Use this for initialization
    void Start()
    {
        nodeScript = GameObject.Find("P16").GetComponent<Node>();

    }

    // Update is called once per frame
    void Update()
    {   
        apple();
        MovePosition();
        //nodeScript.FinalPath.ForEach(x => Debug.Log(x));
        //Debug.Log(nodeScript.FinalPath.Count);
    }
    void apple()
    {
            if (gameObject.transform.localPosition.y == finP[1].transform.localPosition.y)
            {
                if (gameObject.transform.localPosition.x > finP[1].transform.localPosition.x)
                {
                Debug.Log("left");
                    direction = Vector2.left;
                }
                else
                {
                Debug.Log("right");
                direction = Vector2.right;
                }
            }
            else
            {
                if (gameObject.transform.localPosition.y > finP[1].transform.localPosition.y)
                {
                direction = Vector2.down;
                }
                if (gameObject.transform.localPosition.y < finP[0].transform.localPosition.y)
                {
                    direction = Vector2.up;
                }
            }       

    }
    public void OnTriggerEnter2D(Collider2D other)
    {

        if (other.gameObject.tag == "pallet")
        {
            finP.Clear();
            foreach(string var in nodeScript.FinalPath)
            {
                Debug.Log("en script " + var);
                finP.Add(GameObject.Find(var));
            }
            Debug.Log(finP[1]);
        }


    }
    void MovePosition()
    {
        transform.localPosition += (Vector3)(direction * Speed) * Time.deltaTime;
    }
}
Husni Salax
  • 1,968
  • 1
  • 19
  • 29
Tom Unsworth
  • 17
  • 1
  • 7
  • Are you sure your list (finP) has two elements in it when you call finP[1].transform ? – auburg Jan 03 '18 at 13:46
  • 1
    Well, it just means that sometimes there's no element in "finP[1]". Simply use debug to figure it out. – MaxB Jan 03 '18 at 13:46
  • 1
    I am curious how this works at all. findP[] looks to be empty when you you first begin the code. – ryeMoss Jan 03 '18 at 13:46
  • my finP list is empty then game loads and a path if found between the two objects, this is then put into finP. do you think it could be because the code show above is running before i can find the path? although if this is the case i made the order in which they execute go in the order script that finds path then this which moves my object. – Tom Unsworth Jan 03 '18 at 14:11

1 Answers1

1

When you hit the "pallet" gameobject you're clearing the list, while also trying to access elements of that list every frame. Although you're adding to the list immediately after, there is probably a frame or two where the list is lacking the required elements. In your apple() function, I'd throw a if (finP.Count < 1) return; and that should fix your problem.

Brandon Miller
  • 1,534
  • 1
  • 11
  • 16
  • I am clearing the list because there will be a new path and don't want the old one in the list aswell as the new one, ill try this now – Tom Unsworth Jan 03 '18 at 14:14