- Home /
Why won't my door open?
So I've written up this code but it doesn't seem to work, what have i done wrong ? Please answer
var hit : RaycastHit;
if(Physics.Raycast (transform.postion,
transform.forward, hit, 5)) {
if(hit.collider.gameObject.tag=="Door"
&& doorIsOpen == false){
currentDoor = hit.collider.gameObject;
Door(doorOpenSound, true, "dooropen", currentDoor);
}
}
Could you insert a Debug.Log("Hooah! "+doorIsOpen);
into your raycast, just so we know that it gets fired and doorIsOpen is false? Another thing is that you have an unnecessary overhead, collider already has tag derived so you don't have to access gameObject. Other than that it's hard to tell at the moment what else could be wrong. If you get the message "Hooah! false" in console, please share the Door function as well so we can investigate further.
if(Physics.Raycast (transform.position, out hit, 5)) {
its position not postion, and out hit
it seems to be ok, write it for an event and check the tag and function parameter
Is this attached to your camera or player ?
And I don't undertand why are you passing so much stuff, information about is the door closed, opened etc should be saved on door itself. Here is an example what you should attach to door.
public bool closed; //you can check in the inspector is it closed or opened
void Start(){
Action();
}
void Action(){ //This is only function which should be called, without passing anything
if(closed){ //door is closed
//play open door animation and sound
closed = false; //sets the door bool as opened
}
else{ //door is opened
//play close door animation and sound, or whatever
closed = true;
}
}
Answer by SubatomicHero · Aug 16, 2013 at 01:01 PM
Its the line:
hit.collider.gameObject.tag
What are you doing with that? You should be assigning it something or removing that line completely.
Right, but the door seems to be opening and closing in the Door() function. That function could be causing problems too, but we haven't seen the code yet.
Your answer
![](https://koobas.hobune.stream/wayback/20220613112327im_/https://answers.unity.com/themes/thub/images/avi.jpg)