The question is answered, right answer was accepted
My Code freezes Unity - not sure what to do...
I'm working on a horror game project and have the following problem: As soon as the player enters a specific zone, he triggers an enemy spawner, which should spawn only 1(!) enemy that hunts the player down. However, as soon as the Player enters the Zone, Unity freezes up and I can only close it via the task manager...don’t know if there is any crash report I could show you guys...
Here are my three scripts, which should be working together (i got two variables "ActivatedCheck" and "BoolStatus" for me to check if everything is working in the editor - just in case you’re wondering ;) ).
Please note, I am an absolute beginner when it comes to coding - I learn something new everyday :) The whole code is done in Javascript.
Here are my three scripts:
PlayerSpawnTrigger.js :
static var spawning : boolean = false;
var hauntMapTrigger: GameObject;
var boolState = 0;
function OnTriggerEnter(other: Collider) //Check if something has entered the trigger (and declares this object in "other"/////
{
if(other.GetComponent.<Collider>().tag == hauntMapTrigger.tag)
{
spawning = true;
boolState = 1;
}
}
function OnTriggerExit(other: Collider) //Check if the Player has exited the trigger///
{
if(other.GetComponent.<Collider>().tag == hauntMapTrigger.tag)
{
spawning = false;
HauntSpawner.functionActivated = false;
boolState = 0;
}
}
HauntSpawner.js :
var Player : Transform;
var MinSpawnPos : float = 5;
var MaxSpawnPos : float = 25;
var EnemyPrefab : GameObject;
var MinEnemies : int = 1;
var MaxEnemies : int = 1;
var HowManyToSpawn : int = 1;
static var functionActivated = false;
var ActivatedCheck = 0;
function Update()
{
///Spawn enemy on Trigger///
if(PlayerSpawnTrigger.spawning == true && functionActivated == false)
{
Spawn();
}
}
function Spawn()
{
var dist : float = Vector3.Distance(Player.position,transform.position);
if(PlayerSpawnTrigger.spawning == true)
{
var position: Vector3 = Vector3(Random.Range(MinSpawnPos, MaxSpawnPos), 147, Random.Range(MinSpawnPos, MaxSpawnPos));
HowManyToSpawn = 1;
for(var i = 0; i < HowManyToSpawn;)
{
Instantiate (EnemyPrefab,position,Quaternion.identity);
}
functionActivated = true;
ActivatedCheck = 1;
}
}
HauntMoving.js :
var Player : Transform;
var MoveSpeed = 4;
var MinDist = 1;
var MaxDist = 15;
function Update()
{
///Moves toward Player////
transform.LookAt (Player.position);
var dist = Vector3.Distance(transform.position,Player.position);
if(dist <= MaxDist)
{
transform.position += transform.forward * MoveSpeed*Time.deltaTime;
}
if(dist <= MinDist)
{
HasSpawned = false;
Destroy(this.gameObject);
}
}
Like I said - I haven't been coding that long, although I’m starting to get a feel for that logical thinking ;)
Any Help would be GREATLY appreciated!
Answer by hexagonius · Mar 18, 2016 at 11:29 AM
your for loop in second script line 35 is not increasing I.
for(var i = 0; i < HowManyToSpawn; i++)
Thank you very much! - that was indeed the problem. I deleted that bit manually, because I thought this would increase the enemy count from my only needed 1 to more ;)
Follow this Question
Related Questions
Game runs in Editor, crashes on startup after build. "Access Violation (0xc0000005)" 3 Answers
Crash Unity 5.3.5 on startup 0 Answers
Access Violation crash 0xc0000005 (only some players) 0 Answers
Find GameObject at Position even if the GameObject is a trigger. 0 Answers
OnCollisionEnter2D not working, Again! 0 Answers