Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Mar 20, 2016 at 08:09 AM by LethianGames for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by LethianGames · Mar 18, 2016 at 11:06 AM · unity 5triggercrashenemyspawn

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0
Best Answer

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++)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LethianGames · Mar 18, 2016 at 12:12 PM 0
Share

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

Answers Answers and Comments

81 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges