I have two scripts.
One script controls the player’s movement and detects the collision between GameObjects such as NPCs, doors, etc:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed = 6.0f;
public float turnSpeed = 60.0f;
public float gravity = 20.0f;
public bool canDo;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
private Animator anim;
void Start ()
{
controller = GetComponent<CharacterController> ();
anim = GetComponent<Animator> ();
}
void Update ()
{
if (canDo == true)
{
if (controller.isGrounded)
{
moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0.0f, Input.GetAxis ("Vertical"));
}
Vector3 facingrotation = Vector3.Normalize (new Vector3 (Input.GetAxis ("Horizontal"), 0f, Input.GetAxis ("Vertical")));
if (facingrotation != Vector3.zero)
{
transform.forward = facingrotation;
}
controller.Move (moveDirection * speed * Time.deltaTime);
moveDirection.y -= gravity * Time.deltaTime;
if (Input.GetKey ("up") || Input.GetKey ("down") || Input.GetKey ("left") || Input.GetKey ("right"))
{
anim.SetInteger ("isMoving", 1);
} else {
anim.SetInteger ("isMoving", 0);
}
}
}
void OnControllerColliderHit (ControllerColliderHit other)
{
if (other.gameObject.CompareTag ("Door"))
{
if (Input.GetKeyDown ("space"))
{
other.gameObject.SetActive (false);
}
}
if (other.gameObject.CompareTag ("NPC"))
{
if (Input.GetKeyDown ("space"))
{
}
}
}
}
And my other script, which controls what the NPC says (I have another already functioning script that does dialogue). It detects which order the text should be displayed based upon the variable whichDialogueBox:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NPCTestSpeechController : MonoBehaviour {
public DisplayTextController myDisplayTextController;
public GameObject dialogueSystem;
public PlayerController myController;
void Start ()
{
myDisplayTextController.whichDialogueBox = 0;
}
void Update ()
{
if (myDisplayTextController.whichDialogueBox == 1)
{
myController.canDo = false;
dialogueSystem.SetActive (true);
myDisplayTextController.nameString = "Jimothy";
myDisplayTextController.speechString = "'Ello! 'ello!";
}
if (myDisplayTextController.whichDialogueBox == 2)
{
dialogueSystem.SetActive (false);
myDisplayTextController.whichDialogueBox = 0;
myController.canDo = true;
}
}
}
How do I go about accessing the scripts variable whichDialogueBox and changing it. This would need to be changeable so that it can be used for all initiation of NPC action. I thought GetComponent may work, accessing the script after checking the tag to see if it’s an NPC and then editing the variable, but I can’t figure it out. I ask for a bit of help and suggestions of how to go about this. Am I doing this wrong? Thanks!