Infinite loop, with an exit.

Hello, So i am currently making a game about a teleporting monster chasing the player (yes a slender clone…) I want the monster to be able to teleport around the player, but I don’t want the player to see the teleportation, so I have made a empty game object that I plan to teleport ahead of the monster, then when it has found a spot where it is not seen by the player, the monster will teleport to that location.
Problem? Well the “finding a place where the player can’t see it” is the problem, kind of makes unity crash. Here’s the script:

using UnityEngine;
using System.Collections;

public class Chase : MonoBehaviour {

    GameObject Marker;
    Renderer MarkerVis;
    bool Visibel;
    NavMeshAgent Agent;
    Vector3 PlayerPos;
    int Randomizer;


    // Use this for initialization
    void Start () {
 
        Marker = GameObject.Find ("Teleport marker");
        Agent = this.GetComponent<NavMeshAgent> ();      
        MarkerVis = Marker.GetComponent<Renderer> ();
        Visibel = false;

    }
 
    // Update is called once per frame
    void Update () {
 
        PlayerPos = GameObject.Find ("Player").transform.position;
        Agent.destination = PlayerPos;

        if (Input.GetKeyDown (KeyCode.H)) {
            do {
                print ("started");
            Randomizer = Random.Range (1, 3);

            if (Randomizer == 1) {
         
                    Marker.transform.position = new Vector3 (Random.Range (PlayerPos.x, PlayerPos.x + 10), 0, Random.Range (PlayerPos.z, PlayerPos.z + 10));
            if (MarkerVis.isVisible == false) {
                        Visibel = false;
                        print ("success");
                    }

                    else  {Visibel  = true;
            }

        }
            else if (Randomizer == 2) {
         
                Marker.transform.position = new Vector3 (Random.Range (PlayerPos.x, PlayerPos.x + -10), 0, Random.Range (PlayerPos.z, PlayerPos.z + -10));

             
                if (MarkerVis.isVisible == false) {
                    Visibel = false;
                    print ("Success");
                }
                    else  {Visibel  = true;
              }
             
            }

         
            } while (Visibel == true);
    }
    }
}

That’s it, please ask questions if needed.
Thanks in advance.
Btw, I have tested if the gameobject can be seen in a normal not-teleporting enviroment, it can.

I think problem is with brackets. My simple counting them is gives me wild results. I suspect you’re doing “{do { } } while(…);” which is essentially infinite loop as do and while are on different levels.

Second problem might be is that MarkerVis.isVisible… From manual:[quote]
When running in the editor, the scene view cameras will also cause this value to be true.
[/quote]But I doubt that will cause infinite loop in this case as there’s break there.

Sorry, the breaks weren’t supposed to be there, just did them for some testing reasons, it basically showed that it will always say it’s visible. The do and while is because I am doing a DoWhile loop, I belive I am doing it right, I hope so…

[quote=“MortisFillius, post:3, topic: 614811, username:MortisFillius”]
The do and while is because I am doing a DoWhile loop, I belive I am doing it right, I hope so…
[/quote]It still loops and crashes, right?
Have you checked if MarkerVis.isVisible is ever false? Because in editor it might never be false.
There’s quite large possibility Renderer.isVisible is set only once each frame; and as you never let it out of that frame its value never changes. Answer on this question has alternative way of checking if renderer is visible.

Yeah, I also tried turning the scene view away from everything. I am most likely going to do one of the alternatives for the feel of the game, but do you think IEnumerator waiting for like 0,1 second would do it?

If I’m right with isVisible being set only once per frame it’ll take you a lot of frames to find any suitable place. Try
WaitForEndOfFrame instead of waiting for 0.1 seconds… At least if you won’t forget to yield it won’t hang unity next time you run it.