• 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 joona374 · Jul 24, 2014 at 03:30 PM · ifwhileelse

How stop a piece of code from running if the conditions are no more met.

Hello, I'm quite new to scripting and i need help with a piece of code. What I'm basically trying to do is that... Lets say I'm cutting a tree in a rpg game. If i move too far away from the tree, i want him to "stop cutting the tree".

 IEnumerator OnMouseDown() 
 {

     float distance = Vector3.Distance(transform.position, pelaaja.position);
     GameObject Player = GameObject.Find("Player");
     GameMaster gameMaster = Player.GetComponent<GameMaster>();

     if(distance <= 1.414214f && gameMaster.gatheringResources == false )
         {
         float choppingTime = Random.Range(chopMin, chopMax);
         print(choppingTime);
         gameMaster.gatheringResources = true;
         yield return new WaitForSeconds(choppingTime);


         Inventory inventory = Player.GetComponent<Inventory>();
         inventory.wood += 1;
         print("You Just Chopped a log");
         gameMaster.gatheringResources = false;
         Destroy(gameObject);
         }
     else
     {
         print("You are either too far or already choping") ;
     }
 } 

So basically what I'm trying to ask is that if the distance grows larger than 1.414214 while the yield is running, how do i make the rest of the code stop from running and instead print something like "you moved too far". I hope you can understand my question. If you need more info go ahead and ask. I'm guessing it has something to do with a while loop, I just couldn't get it to work

oh also if you wonder what "pelaaja" means, its just player in Finnish.

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

· Add your reply
  • Sort: 
avatar image
0

Answer by SirCrazyNugget · Jul 24, 2014 at 04:05 PM

I'd do something like:

 void OnMouseDown(){
   float endChoppingTime = Time.time + Random.Range(chopMin, chopMax);
   //begine chopping
   DoChopping(endChoppingTime);
 }
 
 IEnumerator DoChopping(float endTime){
 
   while(Time.time < endTime){
     if(distance <= 1.414214f &! gameMaster.gatheringResources){
       //continue chopping
       yield return new WaitForEndOfFrame();
     }else{
       //stop chopping
       yield break;
     }
 
   }else{
     //finished chopping
     yield break;
   }
 }

Comment
Add comment · 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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Else and If 2 Answers

Both if and else are running, because else condition is met after if 1 Answer

Exiting an if statement when condition has been met 1 Answer

C# Check for boolean value with while loop in Update function 1 Answer

How to finish my else if statement? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges