Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Script not able to call function

Discussion in 'Scripting' started by BoxTraininc, Mar 20, 2022.

  1. BoxTraininc

    BoxTraininc

    Joined:
    Mar 11, 2020
    Posts:
    64
    So there are no errors in this script and everything is working fine and dandy. The only problem is my public void NotShoot isn't being called once the player has left the trigger collider. Can someone help me with this?

    Code for reference

    SJ_sesnor

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SJ_sensor : MonoBehaviour
    6. {
    7.  
    8.     void OnTriggerEnter(Collider other)
    9.     {
    10.         if (other.tag == "Player")
    11.         {
    12.             this.GetComponent<Target>().Shoot();
    13.         }
    14.         if (other.tag != "Player")
    15.         {
    16.             this.GetComponent<Target>().NotShoot();
    17.  
    18.         }
    19.     }
    20.  
    21. }
    22.  
    Target

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class Target : MonoBehaviour
    7. {
    8.  
    9.     public float health = 10f;
    10.     public GameObject Player;
    11.     public GameObject You;
    12.  
    13.     public Animator animator;
    14.  
    15.     public float dist;
    16.  
    17.     public float vision = 15f;
    18.  
    19.     public Transform target;
    20.  
    21.     public bool Isee;
    22.  
    23.     void start()
    24.     {
    25.         animator.SetBool("InRange", false);
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         if (Isee == true)
    31.         {
    32.             transform.rotation = Quaternion.LookRotation(transform.position - target.position);
    33.         }
    34.     }
    35.  
    36.     public void Shoot()
    37.     {
    38.         animator.SetBool("InRange", true);
    39.         Vector3 lookAt = target.position * -1;
    40.         Isee = true;
    41.     }
    42.  
    43.  
    44.     public void NotShoot()
    45.     {
    46.         animator.SetBool("InRange", false);
    47.         Isee = false;
    48.     }
    49.  
    50.  
    51.     public void TakeDamage(float amount)
    52.     {
    53.         health -= amount;
    54.         if (health <= 0f)
    55.         {
    56.             Die();
    57.         }
    58.     }
    59.  
    60.     void Die()
    61.     {
    62.         Destroy(gameObject);
    63.     }
    64.  
    65. }
    66.  
     
  2. D12294

    D12294

    Joined:
    Oct 6, 2020
    Posts:
    81
    Hello,

    it's not clear to me what you want achieve. Do you want to call NotShoot when colliding with objects aren't tagged as "Player" or did you want to call NotShoot when exit an object with a "Player" tag? Then you need to use OnTriggerExit.

    Greetings
     
  3. BoxTraininc

    BoxTraininc

    Joined:
    Mar 11, 2020
    Posts:
    64
    Well, what I need to do is call NotShoot once the player leaves the enemies collider and for some reason it doesn't
     
  4. D12294

    D12294

    Joined:
    Oct 6, 2020
    Posts:
    81
  5. BoxTraininc

    BoxTraininc

    Joined:
    Mar 11, 2020
    Posts:
    64
    D12294 likes this.