hi guys, weird thing happening to me rn.
i've created a text object that instantiates every time the player's collider collides with an NPC's, and destroys itself when colliding = null. this works fine, however, every time i start playing, the prefab instantiates twice and stays there. if the player collides with the NPC, it instantiates a third prefab and destroys itself when the player moves and the triggering = null, however the 2 prefabs remain no matter what.
they only destroy themselves when i stop playing.
here are the scripts for the player and the spawner (the NPC's script has no reference to the spawner):
Spawner:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TextSpawner : MonoBehaviour
{
public GameObject floatingTextPrefab;
GameObject newText;
void Start()
{
}
public void Update()
{
}
public void OnTriggering()
{
Vector3 spawnPosition = new Vector3(-3.75f, 2.5f, -133.1f);
newText = (GameObject)Instantiate (floatingTextPrefab, spawnPosition, Quaternion.identity);
}
public void OnNotTriggering()
{
Destroy(newText);
}
}
player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private GameObject triggeringNpc;
private bool triggering = false;
// public GameObject FloatingTextPrefab;
void Start()
{
}
void Update()
{
if (triggering = true)
{
if (Input.GetKey("e"))
{
print("player is interacting with " + triggeringNpc);
}
}
}
//this sentence means that the program will check if the player is triggering with an NPC
void OnTriggerEnter(Collider other)
{
//this statement comes to check if the 'other' (which is the object we are colliding with) is tagged as "NPC", triggering is going to happen
if(other.tag == "NPC")
{
triggering = true;
//this next line means the the other thing we are interacting with and it's definition is the triggering NPC private variable.
triggeringNpc = other.gameObject;
}
FindObjectOfType<TextSpawner>().OnTriggering();
}
//this sentence means that the program will check if the player isn't triggering with an NPC
void OnTriggerExit(Collider other)
{
if (other.tag == "NPC")
{
triggering = false;
triggeringNpc = null;
FindObjectOfType<TextSpawner>().OnNotTriggering();
}
}
}