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

Disabling a script when entering a trigger (javascript)

Discussion in 'Scripting' started by USSTylenol, Oct 8, 2014.

  1. USSTylenol

    USSTylenol

    Joined:
    Sep 28, 2013
    Posts:
    8
    A trigger-that-disables-scripts question, but applying what I've read on google and here hasn't yielded any results yet. (Apologies, haven't figured out how to format code yet).

    I want to turn my spotlight function (attached to my spotlight) off, and move the spotlight itself to a different position and change colour upon entering the trigger zone. The trigger zone's "is trigger" is checked, and the player object has a character collider attached. The variables have open slots in the editor, with the player object, light, light in each respectively. What am I missing?


    var PassingThruObject : Collider;
    var Light : Light;
    var SpotlightSwitch : SpotlightSwitch;

    function Start(){
    SpotlightSwitch = GetComponent("SpotlightSwitch");
    }

    function OnTriggerEnter(collider : Collider){
    if(collider != PassingThruObject){
    Debug.Log("nothing");
    return;
    }
    if(collider == PassingThruObject){
    Debug.Log("Passed Thru");
    SpotlightSwitch.enabled = false;
    Light.color = Color(0, 100, 255);
    Light.transform.position = Vector3(0, 60, 0);
    Light.transform.rotation = Quaternion.Euler(90, 0, 0);
    }
    }


    For reference, my spolight code:

    var pace : float;
    var spotlight : Light;

    function Start () {
    InvokeRepeating("lightSwitch", 2, pace);
    }

    function ranNumGen() {
    var number = Random.value;
    return number;
    }

    function lightSwitch(){
    if(ranNumGen() <= 0.6){
    spotlight.enabled = true;
    Debug.Log("enabled at " + Time.time + "seconds");
    } else {
    spotlight.enabled = false;
    Debug.Log("disabled at " + Time.time + "seconds");
    }
    }
     
  2. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Code (csharp):
    1. You can type [ code ]...[ /code ] around your code.
    2. Or use the button on the button bar to the left of the 3.5" floppy disk icon to insert code.
     
  3. Cpt Chuckles

    Cpt Chuckles

    Joined:
    Dec 31, 2012
    Posts:
    86
    as far as i know, setting a MonoBehaviour.enabled to "false" only stops it from calling Update() or FixedUpdate(), which is not what's going on in your Spotlight code.

    Instead of setting SpotlightSwitch.enabled = false;
    you should say SpotlightSwitch.CancelInvoke("lightSwitch");

    i think
     
  4. USSTylenol

    USSTylenol

    Joined:
    Sep 28, 2013
    Posts:
    8
    Thank you Graham for the coding out help.
    Thank you Cpt Chuckles for the CancelInvoke note, but nothing's changed. My script still runs, and my light doesn't change colour or position.

    According to my debug, the trigger object (an invisible cboid stretched across an entrance) is not registering anything as having passed through. Is it to do with how I've written the collision code?
    Is what I'm trying to do impossible (ie. I need a 2nd spotlight to enable, or I need to spread this code out over the different objects themselves rather than trying to attach it to the invisible waiting cube?)

    With coding out:
    Code (JavaScript):
    1. var PassingThruObject : Collider;
    2. var Light : Light;
    3. var SpotlightSwitch : SpotlightSwitch;
    4.  
    5. function Start(){
    6. SpotlightSwitch = GetComponent("SpotlightSwitch");
    7. }
    8.  
    9. function OnTriggerEnter(collider : Collider){
    10. if(collider != PassingThruObject){
    11. Debug.Log("nothing");
    12. return;
    13. }
    14. if(collider == PassingThruObject){
    15. Debug.Log("Passed Thru");
    16. SpotlightSwitch.enabled = false;
    17. Light.color = Color(0, 100, 255);
    18. Light.transform.position = Vector3(0, 60, 0);
    19. Light.transform.rotation = Quaternion.Euler(90, 0, 0);
    20. }
    21. }
    22.  
    Spotlight Code:
    Code (Javasript):
    1.  
    2. var pace : float;
    3. var spotlight : Light;
    4.  
    5. function Start () {
    6. InvokeRepeating("lightSwitch", 2, pace);
    7. }
    8.  
    9. function ranNumGen() {
    10. var number = Random.value;
    11. return number;
    12. }
    13.  
    14. function lightSwitch(){
    15. if(ranNumGen() <= 0.6){
    16. spotlight.enabled = true;
    17. Debug.Log("enabled at " + Time.time + "seconds");
    18. } else {
    19. spotlight.enabled = false;
    20. Debug.Log("disabled at " + Time.time + "seconds");
    21. }
    22. }
     
  5. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    If in your first script you add a line 9-and-a-half which just does a debug.log does it ever print anything out? I suspect that the problem is just that somethings stopping the trigger from getting called. Is this first script attached to your game object that is the trigger?
     
  6. USSTylenol

    USSTylenol

    Joined:
    Sep 28, 2013
    Posts:
    8
    I get from the start the debug.log the printed comment "nothing", which I expect, as nothing is passing through the invisible cuboid collider.

    This is maybe where I am getting confused. The first script is attached to the invisible cuboid collider. The idea is that when the player passes through this space, the first script is activated. I thought this collider was the trigger. Do I need to attach it to the player?

    (I have read a few guides but still have trouble making out what collides with what and resultingly what triggers which).
     
  7. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Normally OnTriggerEnter() is only called when something enters the trigger. Print the name of the collider.
     
  8. USSTylenol

    USSTylenol

    Joined:
    Sep 28, 2013
    Posts:
    8
    Ok, found the reason why I was getting nothing at the start - the trigger was making contact with the surrounding floor and walls. Thank you for helping me realise that!

    Ok, according to the debug log, the collider is Joan ie. the playable character assigned as PassingThruObject of type Collider. .

    Why is the statement in the if clause not true then? And should Joan be the collider or should the invisible cuboid be?
    Code (Javascript):
    1.  
    2. function OnTriggerEnter(collider : Collider){
    3.     ...
    4.     if(collider == PassingThruObject){
    5.      ...
    6.  
     
  9. USSTylenol

    USSTylenol

    Joined:
    Sep 28, 2013
    Posts:
    8
    Right, made some progress
    I added a rigidbody to the (invisible cuboid trigger) gameobject, and the trigger is being activated. SpotlightSwitch's call to CancelInvoke also stopped progress, but cancelling it out gets me my light changes. Thanks for the help earlier!