What is wrong with this very simply scripting tutorial for a door button combo

I’m trying to follow this very simple tutorial for a door/button combo that doesn’t use animations.

http://videogamedesign24.com/tutorial-opening-doors-using-buttons-triggers-in-unity/

No matter what I try I get three errors on the small script for the button. I can find no way to copy/paste these errors from Unity, and every time I try this webpage deletes this question. This is the fourth time I tried it, so I’m not going to try pasting the errors again.

If someone can follow this tutorial and let me know if they got it to work, or can give me a script for a door and button that will do that same thing it would be appreciated and I can learn from the example.

I will try to answer as best as I can based on some assumptions:

  1. You’re using a 5.x version of unity

  2. You followed the tutorial 100%

Without getting into too much details, first problem is that in TriggerButton class you try to access Door class through GetComponent method. You don’t have to do it, as the Door class is public and you can access it directly like so (and assuming you drag the door object from Hierarchy into the script):

public Door door;

After that, you can start your courutine with:

StartCoroutine (door.OpenUp());

Now you should have 2 files:

  1. Door.cs

        using UnityEngine;
        using System.Collections;
        public class Door : MonoBehaviour
        {
        	public Vector3 OpenPosition;
        	private bool open;
        
        	public IEnumerator OpenUp ()
        	{
        		if (!open)
        		{
        			while (transform.position != OpenPosition)
        			{
        				transform.position = Vector3.MoveTowards(transform.position, OpenPosition, 0.25f);
        
        				if (Vector3.Distance(transform.position, OpenPosition) <= 0.25f)
        				{
        					transform.position = OpenPosition;
        					open = true;
        				}
        				yield return null;
        			}
        		}
        	}
        }
    
  2. TriggerButton.cs

    using UnityEngine;
    using System.Collections;
    public class TriggerButton : MonoBehaviour
    {
    	public Door door;
    
    	void OnTriggerEnter (Collider collider)
    	{
    		if (collider.gameObject.tag == "Player") {
    			StartCoroutine (door.OpenUp());
    		}
    	}    
    }
    

Without getting too much into how it could be done differently, there is a couple of things that you need to make sure of, for this tutorial to work:

  1. Name of your script files, just like the classes (Door.cs and TriggerButton.cs)
  2. Make sure the button object in your scene has ‘Is Trigger’ checked on it’s collider
  3. Make sure you use a player controller with a rigidbody (the OnTriggerEnter method of MonoBehavior requires rigidbody and collider ref: Unity - Scripting API: MonoBehaviour.OnTriggerEnter(Collider))
  4. Make sure your player controller has a ‘Player’ tag selected (your will see it on the top of your Inspector window when you click on the player controller in Hierarchy window). That is because in your TriggerButton class you’re checking the colliders tag.

With that done, it should work without any hickups. Checked on Unity v 5.3.4f1.

Have fun and don’t give up :slight_smile:
Mac