- Home /
Scripting woes, door will not open
How can I get this to work, every time I plug in the designated script for the door to open that is detailed in the unity essentials book it then has supposed "unknown identifiers- doorIsOpen etc" galore even though the people using it have all said the contrary that it supposedly always works...
function OnControllerColliderHit (hit : ControllerColliderHit) { if (hit.gameObject.tag == "outpostDoor" && doorIsOpen == false) { currentDoor = hit.gameObject; Door(doorOpenSound, true, "dooropen", currentDoor); }
var hit : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit, 5))
{
if(hit.collider.gameObject.tag == "outpostDoor" && doorIsOpen == false)
{
currentDoor = hit.collider.gameObject;
Door(doorOpenSound, true, "dooropen", currentDoor);
Please help.
For future, please, don't say "hell" AND format your code, as I'm skipping the question because I can't even read it...
Answer by Statement · Jan 11, 2011 at 06:06 PM
I won't be bothered reading the whole article, but I can provide my own implementation for opening/closing doors.
First is the script for the door. It is quite minimal. OnInteract is what the door will do when a user interacts with it. Here you might want to add more features such as playing a sound with audio.Play() for example. You can make all sorts of scripts that have OnInteract, with this approach. Maybe you want to make a button? Maybe you want to make a car you can enter. All of this can make use of this basic skeleton.
// Door.js var isOpen : boolean;
function OnInteract() { var anim = isOpen ? "CloseDoor" : "OpenDoor"; animation.Play(anim); isOpen = !isOpen; }
Then is the script for the player, that interact with the door. I chose to query the main camera to avoid restrictions that you must put it on a camera and for ease of use. range variable is how far the player can reach. blockingLayers are which objects block a raytest. This can be useful if you have certain triggers that would block the ray.
// Interacter.js var range : float = 5; var blockingLayers : LayerMask = -1;
function Update() { if (Input.GetKeyDown(KeyCode.E)) Interact(); }
function Interact() { var hit : RaycastHit; var ray : Ray = Camera.main.ViewportPointToRay(Vector3(0.5, 0.5, 0)); var options = SendMessageOptions.DontRequireReceiver;
if (Physics.Raycast(ray, hit, range, blockingLayers))
hit.collider.SendMessage("OnInteract", options);
}
It is quite minimalistic and I hope you have a better chance learning scripting this way. Free code is frowned upon, but I also realize when you are struggling the most and want to come to a quick resolution to keep the spirits high. I provide this code free to you this time, but don't expect to always be lucky! :)
I hate to break it to you but this will not work in my fps script:
Assets/Standard Assets/Character Controllers/Sources/Scripts/FPSInputController.js(30,10): BCE0044: expecting (, found 'Update'.
Assets/Standard Assets/Character Controllers/Sources/Scripts/FPSInputController.js(30,10): BCE0044: expecting (, found ''.
$$anonymous$$issingComponentException: There is no 'Animation' attached to the "door" game object, but a script is trying to access it. You probably need to add a Animation to the game object "door". Or your script needs to check if the component is attached before using it. UnityEngine.Animation.Play (System.String animation) (at E:/BuildAgent/work/71ca6fec1b41cc30/Runtime/Export/Generated/Animations.cs:393) Door mech.OnInteract () (at Assets/Door mech.js:9)
I have no idea what you did wrong in your first comment. The code works fine on my machine. $$anonymous$$aybe you pasted it wrong. I think the last error messages said just what I will tell you too. This all require that you have an animation component on the door. Read the errors, they aren't that hard to read. Sure, there is some strange numbers but most errors provide useful information.
The code I presented are supposed to go into new code files. This is complete code. It doesn't have to merge with anything old.
"This all require that you have an animation component on the door." Then why did you leave it out if its part of the function? Also its not like I haven't read it it clearly states that some animation component is left out of the function ;)
Answer by Zylar · Jan 11, 2011 at 08:01 AM
Check where it is you declared the variable doorIsOpen. If it is inside of a function, then that means it'll only be known inside of that function. Your OnControllerColliderHit function can't find it because some other function is stealing all of doorIsOpen's attention.
Don't declare it inside of a function if you want more than one function to talk to it.
Answer by Nicholi · Jan 11, 2011 at 05:30 PM
No, you misunderstood, I have tired that and every other possible valid series of scripts for this I could find on this site and it still doesn't work and the book clearly states that you are suppose to set these functions inside /* then supposedly it will work..
Could you or anyone bestow to me a formatted script that will work. I tried both methods in this book and neither work for me.
Fyi the ones found here : http://www.packtpub.com/article/unity-game-development-interactions-part2
Well, I formatted YOUR script. It isn't even a complete function.
So how exactly is it my script and not even a complete function if its whats published in the unity essentials book?
Well you posted it. You are using it. Hence I call it yours. I can't check every book for reference. All I know, the code posted didn't work and formatting that code (so it was even readable) revealed that the function wasn't complete. I doubt the book would offer broken scripts, but maybe they presented it in fragments that were hard to put together?
Wrong the book states that its a complete function, it doesn't work for me for whatever reason probably has to do with placement issues as the book is a bit vague with its "few lines down from Update()" instructions
Your answer
![](https://koobas.hobune.stream/wayback/20220613094917im_/https://answers.unity.com/themes/thub/images/avi.jpg)