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

Bug Disable/Enable script via OnTriggerEnter or OnTrigger Exit

Discussion in 'Scripting' started by westergard, May 4, 2023.

  1. westergard

    westergard

    Joined:
    May 4, 2015
    Posts:
    79
    Hi, i'm trying to enable/disable a script via an OnTrigger action. Logically it would be simple and i don't know what i'm missing in the script. The console errors doesn't lead me anywhere also?? Is it ok to call a script enable via OnTrigger?


    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class AttackinRange : MonoBehaviour
    5. {
    6.     NavMeshFollower scripttoactivate; // rename for the script
    7.  
    8.     void Awake()
    9.     {
    10.         scripttoactivate = GetComponent<NavMeshFollower>(); // link the component to the variable
    11.     }
    12.  
    13.     void OnTriggerEnter(Collider collisionzone)//when the collider enters the trigger
    14.     {
    15.         if (collisionzone.tag == "Player") // if this object has this tag
    16.         {
    17.             scripttoactivate.enabled; // enable the script
    18.         }
    19.     }
    20.  
    21.     void OnTriggerExit(Collider collisionzone)//when the collider exits the trigger
    22.     {
    23.         if (zonedecollision.tag == "Player") // if this object has this tag
    24.         {
    25.             !scripttoactivate.enabled; // disable the script
    26.         }
    27.     }
    28. }
    the error it gives me is error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement. Is a script not part of this?

    Thanks a lot in advance for your help
     
  2. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    Something that should be very clear to you is that your question has nothing to do with a compilation error. You don't get compilation errors for things you're not supposed to call at a certain point in time; those would be runtime errors if it were something detected.