Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by lhaggot · Aug 31, 2021 at 11:58 PM · crashingtime.deltatimetime.timescale

Why is my Unity Game crashing whenever Time Left is close to 0?

So, currently, in my game, I'm trying to make a Game Manager that does all of the background management and running. Currently, all it does it make sure that some variables are met, and then spawn in a new character and run their AI so that they can fulfill the rest of their tasks. The code looks like this:

public GameObject Person;

 public Transform Spawn1;
 public Transform Spawn2;

 public int PlayersInStore;

 public float TimeLeft = 5;

 public bool Ready = false;


 // Start is called before the first frame update
 void Start()
 {
     TimeLeft = Random.Range(5.0f, 15.0f);
 }

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

     if(TimeLeft > 0.0f)
     {
         TimeLeft -= Time.deltaTime;
     }
     if (TimeLeft <= 0.0f)
     {
         Ready = true;
     }
     if (PlayersInStore < 5 && Ready == true)
     {
         SendInPlayer();
         Ready = false;
     }
 }

 public void SendInPlayer()
 {
     PlayersInStore = PlayersInStore + 1;

     int WhichSpawn = Random.Range(1, 3);
     Transform SpawnPoint = Spawn1;
     Transform FaceTowards = Spawn2;
     
     if(WhichSpawn == 1)
     {
         SpawnPoint = Spawn1;
         FaceTowards = Spawn2;
     }
     if (WhichSpawn != 1)
     {
         SpawnPoint = Spawn2;
         FaceTowards = Spawn1;
     }


     GameObject Player = Instantiate(Person, new Vector2 (SpawnPoint.position.x, SpawnPoint.position.y), Quaternion.identity);
     Player.SetActive(true);

     TimeLeft = Random.Range(5.0f, 15.0f);

     Player.GetComponent<PlayerAI>().SpawnedIn();
 }

The problem is that every time I run my code, whenever Time Left becomes really low (around 0.02f) it will crash Unity, nothing in the engine will be interactable and I can't stop running the game. The only way to fix it is to go into Task Manager and Force Shutdown Unity, then restart it. I don't know what the problem is, I think it may have to do with the if statements in the Update() function, but I have no clue and I don't want to have to keep restarting and crashing Unity when I could just come here and see if anyone knows how to fix it.

Comment
Add comment · Show 1
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 lhaggot · Sep 01, 2021 at 01:37 AM 0
Share

Even without the first if statement that checks if TimeLeft > 0, the timer still stops just above 0. So the Update() function must have stopped being called with the crash, still not sure what is causing it though.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

169 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Slow Motion Issues! 1 Answer

How to Make Timer (Score) Count Up Faster? 1 Answer

Time responding different when playing on Android Device (the APK, not Anroid Remote) 1 Answer

use Time.timescale = 0 with time.deltatime problem 1 Answer

Countdown variable modified with deltaTime continuously being reset 1 Answer


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