If statement not working correctly.

Hello, I’ve tried switching what it was checking, But when it does move, it keeps moving for ever. Help?

using UnityEngine;
using System.Collections;

public class doorScript : MonoBehaviour {

	public GameObject top;
	public GameObject bottom;
	public bool open;

	// Use this for initialization
	void Start () {
		StartCoroutine (doorCheck ());
	}
	
	// Update is called once per frame
	IEnumerator doorCheck () {
		if (open) {
			if (top.transform.position.y < 3.49f) {
				top.transform.Translate (Vector3.up * 0.005f);
			}
			if (bottom.transform.position.y > -3.6749f) {
				bottom.transform.Translate (Vector3.down * 0.005f);
			}
		}
		yield return new WaitForSeconds (0.0125f);
		StartCoroutine (doorCheck ());
	}
}

It works for me. It’s an odd use for co-routines though. It looks like you’re trying to re-implement Update:

    // Update is called once per frame
    void Update()
    {
        if (open)
        {
            if (top.transform.position.y < 3.49f)
            {
                top.transform.Translate(Vector3.up * 0.005f);
            }
            if (bottom.transform.position.y > -3.6749f)
            {
                bottom.transform.Translate(Vector3.down * 0.005f);
            }
        }
    }

Change the transform.Position to transform.localPosition and you might need to add Space.Self to your Translate calls. After that the code worked fine in my testbed.

You might want look into easing. Unity has a couple of really great plugins(iTween, HOTween, LeanTween etc.) that make this kind of animation easier to write and much nicer.

I think you should add an Animator component. But if you relly want to stick with code only, I’d say first you need to launch a Coroutine only when you need to open door instead of doing a perpetual test for nothing.

Then it’s simple:

public IEnumerator OpenDoor()
{
    float speed = 1;
    //speed is a float to tweak to achieve the result you want

    while(top.transform.position.y < 3.49f)
    {
        top.transform.Translate (Vector3.up * Time.deltaTime * speed); 
        bottom.transform.Translate (Vector3.down * Time.deltaTime * speed);   
        yield return null;
    }
}

//Then you can call it from another script like you would for any public function, for example:

StartCoroutine(doorScript.OpenDoor());

If your code still run for ever you should check for your 3.49f value to be right. Actually i’d rather use 2 Vector3 in the method that would be calculated in the beginning of the Coroutine to have the ended position like this:

 public IEnumerator OpenDoor()
 {
     float speed = 1;
     Vector3 topEndPosition=top.transform.position;
     topEndPosition.y+=3;
     Vector3 bottomEndPosition=bottom.transform.position;
     bottomEndPosition.y-=3;
     while(Vector3.Distance(top.transform.position, topEndPosition)>0.05f)
     {
         top.transform.position=Vector3.Lerp(top.transform.position, topEndPosition, Time.deltaTime * speed);
         bottom.transform.position=Vector3.Lerp(bottom.transform.position, bottomEndPosition, Time.deltaTime * speed);
         yield return null;
     }
 }