Search Unity

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

Discussion in 'Prefabs' started by SassyPantsy, Jul 27, 2020.

  1. SassyPantsy

    SassyPantsy

    Joined:
    May 17, 2020
    Posts:
    142
    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):
    player:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.     private GameObject triggeringNpc;
    8.     private bool triggering;
    9.    
    10.     // public GameObject FloatingTextPrefab;
    11.  
    12.     void Update()
    13.     {
    14.         if (triggering)
    15.         {
    16.             if (Input.GetKey("e"))
    17.             {
    18.                 print("player is interacting with " + triggeringNpc);
    19.  
    20.             }
    21.         }
    22.  
    23.     }
    24.  
    25.     //this sentence means that the program will check if the player is triggering with an NPC
    26.     void OnTriggerEnter(Collider other)
    27.     {
    28.      
    29.         //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
    30.         if(other.tag == "NPC")
    31.         {
    32.             triggering = true;
    33.             //this next line means the the other thing we are interacting with and it's definition is the triggering NPC private variable.
    34.             triggeringNpc = other.gameObject;
    35.         }
    36.         FindObjectOfType<TextSpawner>().OnTriggering();
    37.     }
    38.  
    39.     //this sentence means that the program will check if the player isn't triggering with an NPC
    40.     void OnTriggerExit(Collider other)
    41.     {
    42.         if (other.tag == "NPC")
    43.         {
    44.             triggering = false;
    45.             triggeringNpc = null;
    46.             FindObjectOfType<TextSpawner>().OnNotTriggering();
    47.  
    48.         }
    49.     }
    50.  
    51. }
    52.  
    spawner:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class TextSpawner : MonoBehaviour
    7. {
    8.     public GameObject floatingTextPrefab;
    9.     GameObject newText;
    10.     void Start()
    11.     {
    12.        
    13.     }
    14.  
    15.     public void Update()
    16.     {
    17.        
    18.     }
    19.  
    20.     public void OnTriggering()
    21.     {
    22.        
    23.         Vector3 spawnPosition = new Vector3(-3.75f, 2.5f, -133.1f);
    24.         newText = (GameObject)Instantiate (floatingTextPrefab, spawnPosition, Quaternion.identity);
    25.     }
    26.     public void OnNotTriggering()
    27.     {
    28.         Destroy(newText);
    29.     }
    30. }
    31.  
    thanks
     
  2. SassyPantsy

    SassyPantsy

    Joined:
    May 17, 2020
    Posts:
    142
    ::Solution::
    put FindObjectOfType<TextSpawner>().OnTriggering(); on the if statement that regards wht happens when triggering object of type "NPC".