I’ve scoured various forums and websites, and I haven’t found anything that works. I have a script attached to multiple game objects, technically several scripts but whatever. Point is, even though these are separate game objects, they still do the same thing even when the player is only interacting with one of the objects. (The game objects in question are npcs, and when the player talks to one they are supposed to stop walking and start talking. But all of the npcs do that).
Please help. I feel like I have tunnel vision or stress or whatever, so I can’t think straight and I’m getting super stressed out for this hopefully simple fix.
[RequireComponent(typeof(BoxCollider2D))]
public abstract class Interactable : MonoBehaviour
{
public bool isInteracting;
public abstract void Interact();
void Awake()
{
isInteracting = false;
}
private void Reset()
{
GetComponent<BoxCollider2D>().isTrigger = true;
}
private void OnTriggerEnter2D(Collider2D other)
{
isInteracting = true;
if (other is BoxCollider2D && other.CompareTag("Player"))
{
other.GetComponent<PlayerMovement>().StartInteraction();
}
}
private void OnTriggerExit2D(Collider2D other)
{
isInteracting = false;
if (other is BoxCollider2D && other.CompareTag("Player"))
{
other.GetComponent<PlayerMovement>().StopInteraction();
}
}
}
public class DialogueUI : MonoBehaviour
{
[SerializeField] private GameObject dialogueBox;
[SerializeField] private TMP_Text textLabel;
private Typewriter typewriter;
public static bool dialogueStarted;
private void Start()
{
dialogueStarted = false;
typewriter = GetComponent<Typewriter>();
CloseDialogueBox();
}
public void StartDialogueUI()
{
if (dialogueStarted == false)
{
dialogueBox.SetActive(true);
}
}
public void ShowDialogue(DialogueObject dialogueObject)
{
StartCoroutine(StepThroughDialogue(dialogueObject));
}
private IEnumerator StepThroughDialogue(DialogueObject dialogueObject)
{
foreach (string dialogue in dialogueObject.Dialogue)
{
yield return typewriter.Run(dialogue, textLabel);
yield return new WaitUntil(() => Input.GetKeyDown(KeyCode.R));
}
dialogueStarted = false;
CloseDialogueBox();
}
public void CloseDialogueBox()
{
dialogueBox.SetActive(false);
textLabel.text = string.Empty;
}
}
public class NPCSpeaking : Interactable
{
private static DialogueUI dialogueUI;
[SerializeField] public DialogueObject[] npcDialogue;
public int iterations = 0;
[SerializeField] public int speakerNumber;
private void Awake()
{
dialogueUI = FindObjectOfType<DialogueUI>();
isInteracting = false;
}
public void PlotDevelopment()
{
iterations++;
}
public override void Interact()
{
dialogueUI.StartDialogueUI();
StartDialogue();
}
public void StartDialogue()
{
if (DialogueUI.dialogueStarted == false)
{
DialogueUI.dialogueStarted = true;
dialogueUI.ShowDialogue(npcDialogue[iterations]);
Debug.Log("dialogueui");
}
}
}
public class NPCMovement : MonoBehaviour
{
bool facingRight = true;
public float speed;
private Animator anim;
static bool isMoving;
public float distance;
internal Transform thisTransform;
internal Vector3[] moveDirections = new Vector3[] { Vector2.right, Vector2.left };
internal int currentMoveDirection;
bool isOnGround = false;
public float slopeFriction;
public LayerMask whatGround;
float i;
Vector3 Scaler;
public static float npcRotation;
private PlayerMovement player;
int f = 0;
public void Awake()
{
i = distance;
isMoving = true;
currentMoveDirection = 0;
anim = GetComponent<Animator>();
thisTransform = this.transform;
Scaler = transform.localScale;
player = FindObjectOfType<PlayerMovement>();
}
void NormalizeSlope()
{
if (isOnGround)
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up, 1f, whatGround);
if (hit.collider != null && Mathf.Abs(hit.normal.x) > 0.1f)
{
Rigidbody2D body = GetComponent<Rigidbody2D>();
body.velocity = new Vector2(body.velocity.x - (hit.normal.x * slopeFriction), body.velocity.y);
}
}
}
void Update()
{
NormalizeSlope();
if (DialogueUI.dialogueStarted == false)
{
if (i > 0)
{
isMoving = true;
thisTransform.position += moveDirections[currentMoveDirection] * Time.deltaTime * speed;
i -= Time.deltaTime;
}
if (i < 0)
{
i = distance;
ChooseMoveDirection();
Flip();
}
}
else
{
isMoving = false;
if (npcRotation == player.playerRotation)
{
Flip();
f += 1;
}
}
if (DialogueUI.dialogueStarted == false && f == 1)
{
Flip();
f -= 1;
}
anim.SetBool("walking", isMoving == true);
}
void ChooseMoveDirection()
{
if (facingRight == false && currentMoveDirection == 1)
{
currentMoveDirection = 0;
}
else if (facingRight == true && currentMoveDirection == 0)
{
currentMoveDirection = 1;
}
}
void Flip()
{
facingRight = !facingRight;
Scaler.x *= -1;
transform.localScale = Scaler;
npcRotation = Scaler.x;
}
}