Scripting Mulitple Doors, One Key

Unity Community,

I am working on a very simple game where all doors will open using a single key, in this case, spacebar. Here's the issue. I don't want them to open at the same time. I have a trigger box on the door and when the player enters the box a GUI Texture prompts them to press space to open. The door stays open even when the player leaves the trigger and will prompt again to close should they choose. However, EACH door on the level opens when any one of the triggers is activated.

How can I script it so that only the door that is attached to the trigger is the one that opens? Help! I'm at my wits end.

Current setup:

  • I'm using prefabs (the doors are different shapes, so a new prefab is needed for each door, which kind of defeats the purpose but neither way works so far)

  • I have a triggerDoor and doorObject script, which sets the trigger and door to recognize each other. But there is an issue with Unity using a globalVar instead of allowing me to use a localVar.

If someone has encountered this problem and knows a workaround, or can link me an existing thread where this issue has been solved, I would greatly appreciate it.

Thanks,

JV

One prefab should do it. Each instance could override the mesh(es) used on the 'proto' prefab. Your script should only affect the object (prefab instance) that it is attached to. You'd want to check your trigger box (I assume this is a box collider with IsTrigger set, so you handle the OnColliderEnter function, right?) So when 'this' collider is entered, open 'this' door.

If you want 'global' vars, you can place scripts into a Plugins folder under Assets (create it if not there already), probably make everything 'static', or you can attach a script to a GameObject (call it 'Globals' or 'Brain' or 'Scripts' or something), and either user GameObject.Find in the Startup of your prefab's script, or declare a var globals : GlobalScript ; (or whatever you'd like to call it), and drag the global object onto that variable in your prefab's settings in the Inspector. I hope that wasn't too confusing.