Having trouble with an enemy script.

I’m making a Slender Man game and so far I’ve gotten all of the mechanics down for slender man and the collecting items but I only have one problem which is causing him to appear before the first page is collected, so the game basically starts off with the player being chased by him. How can I fix this? My script is below, and I have a feeling it might have something to do with the “meshSlender.enabled” being true or false in certain places but I can’t really figure it out. Thanks in advance and here is the script.

using UnityEngine;
using System.Collections;

public class SlenderBehaviour : MonoBehaviour {

private PlayerBehaviour player;
public float radiusToSpawn = 30;
public float minimumRadius = 5;
private float currentRadiusToSpawn;
public Renderer meshSlender;
private bool spawned;
public float scareFactory = 0.01f;
private float currentScareFactory;
public float distanceToAffect;

public float timeToSpawn;
private float currentTimeToSpawn;

private float distancePlayer;
private GameController gameController;

// Use this for initialization
void Start () {
	player = FindObjectOfType (typeof(PlayerBehaviour)) as PlayerBehaviour;
	meshSlender.enabled = false;

	gameController = FindObjectOfType (typeof(GameController)) as GameController;

	currentScareFactory = scareFactory;
	currentRadiusToSpawn = radiusToSpawn;

}

// Update is called once per frame
void Update () {

	transform.LookAt (player.transform);

	currentTimeToSpawn += Time.deltaTime;
	if(currentTimeToSpawn > timeToSpawn /*&& gameController.totalObjectivesOk > 0*/){
		currentTimeToSpawn = 0;

		Spawn();
	}

	distancePlayer = Vector3.Distance (transform.position, player.transform.position);

	Ray tempRay = new Ray(transform.position, Vector3.forward);
	bool rayCastFail = true;
	RaycastHit hitInfo;
	if (Physics.Raycast (tempRay, out hitInfo)) {
		if (hitInfo.collider.tag == "Player") {
			rayCastFail = false;
		}
	}

	if (distancePlayer < distanceToAffect && meshSlender.isVisible && rayCastFail) {
		player.scare += currentScareFactory / distancePlayer;

	}

	//Calculation of damage that the Slender will give the Player
	if (gameController.totalObjectivesOk > 0) {

		currentRadiusToSpawn = radiusToSpawn - (radiusToSpawn / gameController.GetTotalObjective () * gameController.totalObjectivesOk);

		if (currentRadiusToSpawn < minimumRadius)
			currentRadiusToSpawn = minimumRadius;

		currentScareFactory = scareFactory + (scareFactory / gameController.GetTotalObjective () * gameController.totalObjectivesOk);

	}

}

public void Spawn(){
	meshSlender.enabled = false;
	Vector3 positionToGo = player.transform.position;

	positionToGo.x += Random.Range (-currentRadiusToSpawn, currentRadiusToSpawn);
	positionToGo.z += Random.Range (-currentRadiusToSpawn, currentRadiusToSpawn);

	positionToGo.y = Terrain.activeTerrain.SampleHeight (positionToGo);

	transform.position = positionToGo;
	spawned = true;
}

void OnBecameInvisible(){
	if (spawned){
		meshSlender.enabled = true;
	    spawned = false;
    }
}

}

First, make sure you are initializing “timeToSpawn”, I guess you are doing this by the editor, but if you are not, it’s going to be 0 by default (since you are not setting it on this code). Then, if this is not the problem, there are some ways to hide/show GameObjects that you can try. You can show/hide a GameObject by deactivating it, for example:

public void Show()
{
    this.gameObject.SetActive(true);
}
        
public void Hide()
{
    this.gameObject.SetActive(false);
}

But in this way you going to activate/deactivate all the components of the GameObject. So if you need the others components active, you should disable only the Renderer Component, as you are doing. But, most of times, our GameObjects are composed by many Renderers, and that includes their children. So you should get every renderer of the GameObject tree and then deactivate them. Something like this:

public void SetVisibility(bool isVisible)
{
    Renderer[] renderers = this.GetComponentsInChildren<Renderer>();
                
    foreach ( Renderer renderer in renderers )
    {
        renderer.enabled = isVisible;
    }
}

You can also separate this last method as Hide() / Show() methods, I just wanted to show a different way. I hope this answer could be useful.

You should use GameObject.setActive() to enable or disable your slenderman.