Int resetting?

So I’m making a conversation system. It displays the text and portraits all fine. My problem is, that my int that keeps track of which line to display doesn’t reset to zero which results in the second line of text displaying first. Here’s my scripts:

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

public class ConversationManager : MonoBehaviour {

	public GameObject dBox;
	public Text dText;

	public bool dialougeActive;

	public string[] dialougeLines;
	public int currentLine;

	public Image Portrait;
	public Sprite newSprite;

	public Sprite[] Portraits;


	void Start(){


	}

	void Update(){
		if (dialougeActive && Input.GetKeyDown (KeyCode.Space)) {

			currentLine++;
		}
		if (currentLine >= dialougeLines.Length) {
			if (currentLine >= Portraits.Length) {
				dialougeActive = false;
				dBox.SetActive (false);
				currentLine = 0;

			}
		}
			
			Portrait.sprite = newSprite;
			newSprite = Portraits [currentLine];

			
			dText.text = dialougeLines [currentLine];
	}

	public void ShowDialouge(){
		dialougeActive = true;
		dBox.SetActive (true);

	}
}

Script that goes on the NPC:

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

public class NPConversation : MonoBehaviour {
	
	public string dialouge;

	public string[] dialougeLines;

	public Sprite Portrait;

	public Sprite[] Portraits;

	private ConversationManager cm;



	// Use this for initialization
	void Start () {

		cm = FindObjectOfType<ConversationManager> ();
	}
	
	// Update is called once per frame
	void Update () {
		
	}
	void OnCollisionStay2D(Collision2D other){
		if(other.gameObject.tag == "Player" && Input.GetKey(KeyCode.Space)){

			if (!cm.dialougeActive) {

				cm.Portraits = Portraits;
				cm.newSprite = Portrait;


				cm.currentLine = 0;
				cm.dialougeLines = dialougeLines;
				cm.ShowDialouge();

			}
		}
	}
}

Hope you understand. Thanks in advance!

Hm perhaps the update function of ConverstaionManager is processed after OnCollisionStay() off the NPC script. In that case the GetKeyDown(space) is going to allow the currentLine++ to trigger right at the start of the conversation. What I woud do is enforce a keyUp event in between each KeyDown.

I think the problem is when you start the conversation. From your code I assume you start an conversation by pressing Space. Here is the problem with that.

// Assume this is the first time your press space/start Dialog (if you start dialog by pressing space)

// currentLine is 0
if (dialougeActive && Input.GetKeyDown (KeyCode.Space)) {
         // Space is pressed
          currentLine++;
       
         // Current line is now 1 (meaning the second line of text in array)
 }

You then continue to call the first line. I suggest one of two things:

** Solution 1: **

Move the code above to the bottom of the update function

** Solution 2: **

In the oncollition script do the following:

// Change the following:
cm.currentLine = 0;

// To the this:
cm.currentLine = -1;