i'm trying to make a dialogue script for a rpg game.
this is my code for now.
#pragma strict
var keyNext : KeyCode;
var text = new Array();
var page = 0;
function OnCollisionEnter2D(col : Collision2D) {
Debug.Log ("COLLISION DETECTED");
if(col.gameObject.name == "NPC") {
text[0] = "hi";
text[1] = "bye";
} else {
Debug.Log("no text for "+col.gameObject.name);
}
Debug.Log(text.length);
if(Input.GetKey(keyNext) && text.length < page) {
page++;
}
Debug.Log(text[page]);
}
function OnCollisionExit2D(col : Collision2D) {
page = 0;
Debug.Log("walked away");
}
function update () {
}
but it only prints 'hi', doesn't go to 'bye' when i press the keyNext key (spacebar)
i hope someone can help me!
You are checking Input.GetKey in OnCollisionEnter2D. Try to move it to Update-function instead, like this: https://docs.unity3d.com/Documentation/ScriptReference/Input.GetKey.html
In addition you might want to consider using GetKeyUp instead of GetKey; https://docs.unity3d.com/Documentation/ScriptReference/Input.GetKeyUp.html