I am getting error "Array Index Is Out of Range"

I am making a really simple game in which i want to take 2 string inputs from player via “Input Field” and compare first and last characters of both strings and if first alphabet of 2nd word is not equal to first alphabet then change the scene.
please please please help me with this
Thank You

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Input1 : MonoBehaviour
    public InputField word1;
    public InputField word2;

    void FixedUpdate()
            string p1word = word1.text;          //taking value from input field to string
            string p2word = word2.text;
            char p1firstltr = p1word[0];           //first characters of p1word
            char p2firstltr = p2word[0];
            char p1lastltr = p1word[p1word.Length - 1];//last characters of p1word
            char p2lastltr = p2word[p2word.Length - 1];

            if (p2firstltr != p1lastltr)    //comparing both characters
                SceneManager.LoadScene(4);  //change scene

            else if (p1firstltr != p2lastltr)

Maybe your string is of length zero.
Check for that before accessing characters in it.

1 Like

Make sure your word1 and word2 are not returning null or empty string.

Try printing word1 and word2 to see if they return null value.
Try changing char p1lastltr = p1word[p1word.Length - 1] to char p1lastltr = p1word.charAt(p1word.length -1);