🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

prefab instantiate twice every time i start playing, in addition to it's "if" instantiate method

Started by
2 comments, last by SassyPantsy 3 years, 11 months ago

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();

        }
    }
  
}

None

Advertisement
FindObjectOfType<TextSpawner>().OnTriggering();

You are calling this regardless of if “other.tag == ”NPC". Thats at least not supposed to be, as its not how the OnTriggerExit-method is implemented. I suppose that might also be the reason why the object is spawned many times.

On another not, I think this implementation is backwards. I would not tie this behaviour to the player, instead I would hookup the TextSpawner so that it is triggered by the NPCs collision-response to the player. The issue with having this code in the player is that if you keep adding features, you might end up with a giant collision-handling routine inside the Player-class, which could much more easily be refactored out into individual collison-handlers that could all belong to their respective scripts (truthfully, I belive the TextSpawner could simply have its own OnTriggerEnter-method as long as its attached to the NPC gameobject).

@Juliean YOU ARE A GENIUS!

thank you so much! i knew there must've been a problem with it triggering the ground or something like that, so i checked the tags instead of doing what you said.

as for the script belonging on the NPC, you're probably right. i'll do that as well.

None

This topic is closed to new replies.

Advertisement