• 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
1
Question by sonic220 · Apr 05, 2011 at 07:00 PM · crashplatformermoving-platform

this script make unity crash

hi guys, I'm still a novice, I know that somehow I've made a mistake, I need some help: I want a platform to move when I call this function, however 1/2 of the time unity crashes. How can I make the platform move to a direction to another if OnTrigger is called by an OnTriggerEnter? I should use a while, but how? Edit: FIXED BY USING YIELD - however now the game seems to freeze every 4 seconds for 0.5 seconds, what's the problem?

var muovi : boolean = true; function OnTrigger() { //muovi = true; if(ontrigger && muovi){ //Debug.Log("urtato"); while(muovi){ transform.position = Vector3.Lerp(transform.position, arrivo, Time.deltaTime * velocita); yield; // l'oldissimo wait(1) if(Vector3.Distance(transform.position, arrivo)<= 1){ muovi = false; } } while(!muovi){ transform.position = Vector3.Lerp(transform.position, partenza, Time.deltaTime * velocita); yield; // l'oldissimo wait(1) if(Vector3.Distance(transform.position, partenza)<= 1){ muovi = false; } }

 }

}

OK, that's very strange, I don't have this problem if i comment (with / and /) this code, everything works fine. WHY!? it's really strange

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 Eric5h5 · Apr 05, 2011 at 07:32 PM

You wrote an infinite loop. Putting a yield statement in the while loop will allow time to pass, so that the loop should work. By the way, you don't need a separate OnTrigger function, you can just put that in OnTriggerEnter, which can be a coroutine.

Comment
Add comment · Show 2 · 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 sonic220 · Apr 05, 2011 at 07:47 PM 0
Share

yeah In fact I've fixed this way :D however something strange happens sometime, seems that the yield freezes the game every 4 seconds for 0.5 seconds, I don't know how to solve this problem, I'll update the code

avatar image Eric5h5 · Apr 05, 2011 at 08:18 PM 0
Share

I don't see anything offhand that would cause that; certainly yield doesn't do that.

avatar image
-1

Answer by flaviusxvii · Apr 05, 2011 at 07:18 PM

You don't want a loop. The engine is already looping for you. Put

transform.position = Vector3.MoveTowards(transform.position, arrivo, Time.deltaTime * velocita * 0.001);
if(Vector3.Distance(transform.position, arrivo)<= 1){
      muovi = false;
}

in Update().

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 Eric5h5 · Apr 05, 2011 at 07:33 PM 1
Share

There's nothing wrong with a loop, and using Update is not what Volt3 wants, since Update runs every frame, and the function is written so that the loop only runs when OnTriggerEnter occurs.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Problem with moving (animated) platforms. need script to keep player object on 1 Answer

Moving Platform 1 Answer

moving platform jumping and then continuing it's path 0 Answers

Platforms again.. Working but.. 0 Answers

2D 360 degress platformer example needed 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