Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 phedon · Jul 29, 2014 at 11:13 PM · coroutinesif statementwhileturn based

TBG,If,While

Im trying to make a turn based game for a project,it will be a remake of the risk game.I have made this code(it is the phase when the players choose their territory) and i m trying to find an elegant way to arrange the phases of the game.My main problem is in the update function.With this if statement it constanty prints the end turn untill the unity crashes,i have tried also with a while loop and some coroutines,but when i run the script whith the whilw loop the unity stops responding,albeit i make the condition false and about the coroutines,i dont understand them very well,but is seems that they are not useful in this point.It will be thankfull if anyone of you could help me and sorry if my english are poor,i think i made my point.

var referenceCamera: Transform;

public var displayText : GUIText;

var hit : RaycastHit;

var playerOneChoose = new Array(); var playerTwoChoose = new Array();

private var Choose = true;

private var playerOne = true;

public var playerOneArmy = new Array();

public var playerTwoArmy = new Array();

function Update (){

     if (Choose == true
     &&playerTwoChoose.length + playerOneChoose.length <= 25){

         ChooseOne();
         ChooseTwo();
     }
     else{
         PhaseChooseEnd();
     }
 

}

function ChooseOne(){

var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);

if (playerOne == true){ displayText.text = "Player One";
if (Input.GetMouseButtonDown(0)) {

      if(Physics.Raycast(ray, hit)
      && hit.collider.renderer.material.color != Color.red){
               hit.collider.renderer.material.color = Color.green;
               playerOneChoose.Add(hit.collider);
               print ("P1");
               playerOne=!playerOne;
               }
       }    
  }

}

function ChooseTwo(){

var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);

if (playerOne == false){ displayText.text = "Player Two"; if (Input.GetMouseButtonDown(0)) {

      if(Physics.Raycast(ray, hit)
      && hit.collider.renderer.material.color != Color.green){
               hit.collider.renderer.material.color = Color.red;
               playerTwoChoose.Add(hit.collider);
               print ("P2");
               playerOne = true;
          }
  }    

}

}

function PhaseChooseEnd(){

if (playerTwoChoose.length + playerOneChoose.length == 26){ Choose = false; print ("end turn");

 }

}

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 gjf · Jul 30, 2014 at 09:05 AM 0
Share

please fix the code tags to contain all of the code

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Arcadewiz · Jul 30, 2014 at 09:24 AM

Use Debug.log("Choose value : " + Choose) at the places in your code where you are trying to check for change in the boolean value.

Place different Debug.Log in your Update if else commands to see when the conditions are being met.

I have a feeling your if in update statement might not be working properly.

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Problems with nested coroutines: while loop freezes 1 Answer

Why this crashes unity ? [Do While & If] 2 Answers

Can you run a coroutine more often than Update? 1 Answer

Platform cannot move up 2 Answers

No mater what I cannot make this GUI button true! please help. 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