Help Me Understand why this Boolean is Not Working

Hi Everyone,

I have had a look around online but am unable to find an answer that works for me. I would have thought this would be pretty simple. But alas it is not.

I have a script that opens a door. The player hovers over the doorknob with the mouse, text pops up saying “Open Door [E]”. Player presses ‘E’ and the door opens. Magic.

The script looks like this:

import UnityEngine.UI;

var TextDisplay : GameObject;
var TheDistance : float = PlayerCasting.DistanceFromTarget;

var TheDoor : GameObject;

function Update ()
{
    TheDistance = PlayerCasting.DistanceFromTarget;
    if (Input.GetButtonDown("Action"))
    {
        if (TheDistance <= 2)
        {
            OpenTheDoor ();            
        }
    }
}

function OnMouseOver () 
{
    if (TheDistance <= 2)
    {
        TextDisplay.GetComponent.<Text>().text = "Open Door [E]";        
    }
}

function OnMouseExit ()
{
    TextDisplay.GetComponent.<Text>().text = "";
}

function OpenTheDoor () 
{    
    TheDoor.GetComponent("Animator").enabled=true;
    yield WaitForSeconds(1.5);
    TheDoor.GetComponent("Animator").enabled=false;      
}

The problem is that when the door is open the polayer can still ‘Open the door’ which resets the animation from the start (the previous position of the door). I figured a boolean value for the doors position would work since it can only be Open or Closed. However, that code won’t seem to work. Here is basically what I am trying to do.

import UnityEngine.UI;

var TextDisplay : GameObject;
var TheDistance : float = PlayerCasting.DistanceFromTarget;

var TheDoor : GameObject;

var DoorOpen : boolean = false;

function Update ()
{
    TheDistance = PlayerCasting.DistanceFromTarget;
    if (Input.GetButtonDown("Action"))
    {
        if (TheDistance <= 2)
        {
            OpenTheDoor (); 
            ChangeDoorStatus ();
        }
    }
}

function OnMouseOver () 
{
    if (TheDistance <= 2)
    {
        if (DoorOpen == false)
        {
            TextDisplay.GetComponent.<Text>().text = "Open Door [E]";             
        }
        else
        {
            TextDisplay.GetComponent.<Text>().text = "Close Door [E]";
        }
    }
}

function OnMouseExit ()
{
    TextDisplay.GetComponent.<Text>().text = "";
}

function OpenTheDoor () 
{    
    TheDoor.GetComponent("Animator").enabled=true;
    yield WaitForSeconds(1.5);
    TheDoor.GetComponent("Animator").enabled=false;      
}

function ChangeDoorStatus ()
{
    if (DoorOpen == false)
    {
        DoorOpen != DoorOpen;
    }
    else 
    {
        DoorOpen == false;
    }
}

Obviously the animation will need to be tweaked when the door is being closed, but you get the idea. Can anyone let me know why this won’t work? Bonus points if you can give me something that works, but mostly I am wantiong to know why this doesnt, so I can learn from it.

Thank in advance.

2 Answers

2

You’re checking the “DoorOpen” flag only in OnMouseOver, but not in Update where you’re actually opening the door.

Also, i would get rid of the ChangeDoorStatus method and explicitly set the door state at the end of “OpenTheDoor” (and later “CloseTheDoor” once you have that).

You are confusing your syntax slightly between comparing values and assigning values.

function ChangeDoorStatus ()
 {
     if (DoorOpen == false)
     {
         DoorOpen != DoorOpen; //Invalid, you would want DoorOpen = true
     }
     else 
     {
         DoorOpen == false;//Invalid, '==' compares, '=' assigns the value.
     }
 }

Correct answer 1:

function ChangeDoorStatus ()
 {
     if (DoorOpen == false)
     {
         DoorOpen  = true; 
     }
     else 
     {
         DoorOpen = false;
     }
 }

Cooler Correct Answer:

function ChangeDoorStatus ()
 {
         DoorOpen  = !DoorOpen; 
   }

Now just remember, when one door closes - another must open.