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 /
avatar image
0
Question by pearsonfrank001 · Jul 25, 2018 at 08:29 PM · loopinfinite

I broke my game with what i think is an infinite loop?

I'm new to Unity, and I was working on a script to control a fire. When the player is close, and the fire is off, player can light the fire by pressing f, when the fire is on, player can turn it off by pressing f. However, when I run the script, the game freezes and i have to restart my laptop to unfreeze it. Can someone explain what I did wrong and how to fix it?

code here

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ScareCrow95 · Jul 25, 2018 at 08:44 PM

There are quite a few things wrong with your code. Can you explain why are you running a for loop upto 100?

And it is an infinite loop, and "GetKey" means it will return true every frame while the key is pressed. You should be using "Getkeydown".

try something like this:(there will be syntax errors, I typed on my phone)

 bool canControlFire;
 
 onTriggerEnter - canControlFire=true; (you can turn on the text hint here)
 ontriggerExit - canControlFire = false;(you can turn off the text hint here)
 
 void Update(){
 if(canControlFire)
 {
 if(Input.GetKeyDown("f"))
 {
 if(fire.isActiveInHiearchy) // is fire active?
 fire.setactive(false);
 else
 fire.setactive(true);
 }
 }
 }

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 pearsonfrank001 · Jul 26, 2018 at 09:56 PM 0
Share

Thanks. I ran the for loop up to 100 because I wasnt sure how to keep the program checking for a key input. But now, as Bystander33 pointed out, I know I could just use the update() function.

avatar image
1

Answer by Bystander333 · Jul 25, 2018 at 09:01 PM

First of all, use GetKeyDown or GetKeyUp instead of GetKey which returns true the entire time the key is pressed. Secondly don't us Coroutines for this - just do it all in Update(). In OnTriggerEnter set a boolean called e.g. playerIsNear to true and set it to false in OnTriggerExit. The in Update,

 if ((playerIsNear) && (GetKeyUp(KeyCode.F))
 {
   fireislit = !fireislit;
   fire.SetActive(fireislit);
 }

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 pearsonfrank001 · Jul 26, 2018 at 09:57 PM 0
Share

Thank you.

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

88 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

Related Questions

Troubles with making a do while loop work 1 Answer

Game random freeze / hang up / stuck 0 Answers

How to debug infinite loop in Visual Studio? 0 Answers

Catching a Stack Overflow 1 Answer

What's wrong with this code and is there an easier way to write it!? 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