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 sib_konst · May 04, 2015 at 10:17 AM · unity 5while-loop

While loop

Hello. I'm trying to write a simple Minesweeper game. I have two scripts. First one scOnCellPress is at prefab, and it contains next lines:

 public var isMouseOver:int=0;
 public var sGameControl:scGameControl;
 public var hasMine:int=0;
 
 function Start () {
     var objGameControl=GameObject.Find("GameControl");
     sGameControl=objGameControl.GetComponent(scGameControl);
 }
 function Update () {
         if (Input.GetMouseButtonUp(0) && isMouseOver==1){
              if (sGameControl.minesSet=="-1"){
               sGameControl.minesSet=gameObject.name;
          }
         }
 }
 function OnMouseOver(){
     isMouseOver=1;
 }
 function OnMouseExit(){
     isMouseOver=0;
 }


Second one scGameControl is at empty GameObject named GameControl at the scene, and contains next lines:

 var x:float;
 var y:float;
 var Field:GameObject[,];
 var preCell:GameObject;
 var cellsNum:int=0;
 var totalMines:int;
 
 function Start () {
     Field=new GameObject[x,y];
     for (var j=0;j<y;j++){
         for (var i=0;i<x;i++){
             var cellClone=Instantiate(preCell,Vector3(0,0,0),Quaternion.identity);
             cellClone.name="Cell"+cellsNum;
             cellsNum+=1;
             Field[i,j]= cellClone;
         }
     minesSet="-1";
 }
 
 function Update () {
     if (minesSet!="-2" && minesSet!="-1" && minesSet!="-3"){
            SetMines();
            minesSet="-3";
         }
 }
 
 function SetMines(){
     var c=0;
     var i:int;
     var j:int;
     while (c<totalMines){
         i=Random.Range(0,x);
         j=Random.Range(0,y);
         if (Field[i,j].name!=minesSet){
             Field[i,j].GetComponent(scOnCellPress).hasMine=1;
             c++;
         }
     }


So mines are generated after first cell at field is opened. totalMines are set by hand at designtime. 10 for example. But at some reason sometimes only 9 mines are generated. I've already tried to rewrite SetMines() like that:

 var c=0;
 var i:int;
 var j:int;
 
 for (c=0;c<totalMines;c++){
         i=Random.Range(0,x);
         j=Random.Range(0,y);
         if (Field[i,j].name!=minesSet){
             Field[i,j].GetComponent(scOnCellPress).hasMine=1;
         }
         if (Field[i,j].name==minesSet){
             c--;
         }
     }

But that didn't solved the problem.

Comment
Add comment · Show 3
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 sib_konst · May 04, 2015 at 09:22 AM 0
Share

Tried to rewrite Set$$anonymous$$ines() like that:

 function Set$$anonymous$$ines(){
     var c=0;
     var i:int;
     var j:int;
     while (c<=total$$anonymous$$ines-1){
         i=Random.Range(0,x);
         j=Random.Range(0,y);
         if (Field[i,j].name!=$$anonymous$$esSet){
             Field[i,j].GetComponent(scOnCellPress).has$$anonymous$$ine=1;
             c++;
         }
         if (Field[i,j].name==$$anonymous$$esSet){
             Debug.Log("Hit");
         }
     }
 }

In case 9 $$anonymous$$es are generated, the log is empty.

avatar image sib_konst · May 04, 2015 at 09:45 AM 0
Share

And if rewrite Set$$anonymous$$ines() like that:

 function Set$$anonymous$$ines(){
     var c=0;
     var i:int;
     var j:int;
     while (c<total$$anonymous$$ines){
         i=Random.Range(0,x);
         j=Random.Range(0,y);
         if (Field[i,j].name!=$$anonymous$$esSet){
             Field[i,j].GetComponent(scOnCellPress).has$$anonymous$$ine=1;
             c++;
         }
         if (Field[i,j].name==$$anonymous$$esSet){
             Debug.Log("Hit");
         }
     }
     Debug.Log("c="+c);
     for (i=0;i<x;i++){
         for (j=0;j<y;j++){
             if (Field[i,j].GetComponent(scOnCellPress).has$$anonymous$$ine==1){Debug.Log(Field[i,j].name+' has $$anonymous$$e!');}
         }
     }
 }

In case when 9 $$anonymous$$es are generated, log tells:

 c=10
 UnityEngine.Debug:Log(Object)
 
 Cell10 has $$anonymous$$e!
 UnityEngine.Debug:Log(Object)
 
 Cell11 has $$anonymous$$e!
 UnityEngine.Debug:Log(Object)
 
 Cell24 has $$anonymous$$e!
 UnityEngine.Debug:Log(Object)
 
 Cell15 has $$anonymous$$e!
 UnityEngine.Debug:Log(Object)
 
 Cell35 has $$anonymous$$e!
 UnityEngine.Debug:Log(Object)
 
 Cell6 has $$anonymous$$e!
 UnityEngine.Debug:Log(Object)
 
 Cell7 has $$anonymous$$e!
 UnityEngine.Debug:Log(Object)
 
 Cell37 has $$anonymous$$e!
 UnityEngine.Debug:Log(Object)
 
 Cell57 has $$anonymous$$e!
 UnityEngine.Debug:Log(Object)

Nine $$anonymous$$es, and counter variable at ten. I really don't get it.

avatar image sib_konst · May 04, 2015 at 09:49 AM 0
Share

And when 10 $$anonymous$$es are generated, counter variable is still at 10.

2 Replies

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

Answer by $$anonymous$$ · May 04, 2015 at 11:22 PM

Seems like it might be a matter of creating a mine where there is already a mine. Note I'm a c# guy so there may be a couple bugs in the code. Try this:

 function SetMines(){
     var c=0;
     var i:int;
     var j:int;
 
     // Create 'totalMines' number of mines
     for(c=0;c<totalMines;c++)
     {
         // keep looping till a mine is placed
         while(true)
         {
             // Get a random point
             i=Random.Range(0,x);
             j=Random.Range(0,y);
 
             // Check if the point is not yet a mine
             if (Field[i,j].name!=minesSet)
             {
                 // Set the Field[i,j] to be a mine
                 Field[i,j].GetComponent(scOnCellPress).hasMine=1;
 
                 // Break out of the current loop and move on to the next mine
                 break;
             }
         }
     }
 }

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 sib_konst · May 05, 2015 at 12:39 AM 0
Share

Wow, I really forgot to check if cell already has $$anonymous$$e.

 function Set$$anonymous$$ines(){
     var c=0;
     var i:int;
     var j:int;
     while (c<total$$anonymous$$ines){
         i=Random.Range(0,x);
         j=Random.Range(0,y);
         if (Field[i,j].name!=$$anonymous$$esSet && Field[i,j].GetComponent(scOnCellPress).has$$anonymous$$ine==0){
             Field[i,j].GetComponent(scOnCellPress).has$$anonymous$$ine=1;
             c++;
         }
     }
 }

Its all works great now. Thanks!

avatar image $$anonymous$$ · May 05, 2015 at 09:15 AM 0
Share

No problem. Glad I could help.

avatar image
0

Answer by siaran · May 04, 2015 at 02:53 PM

On Phone so bit short but youb should use smaller or equal sign instead of smaller or set amount of mines 1 higher then the number you actually want

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 sib_konst · May 04, 2015 at 04:07 PM 0
Share

Correct me if I'm wrong, but itterating from zero to less than number_of_$$anonymous$$es should work fine too. And the point is that at about one launch from five there is nine $$anonymous$$es generated, not ten, but in other four launches there is all ok: ten $$anonymous$$es are generated.

avatar image siaran · May 04, 2015 at 04:55 PM 0
Share

Sorry you v are right it should work didn't v read it properly formatting is bad on phone. $$anonymous$$ost likely there is a problem with the c if condition in your loop have you checked that. ?

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

how do I move array objects to world coordinates? 1 Answer

Index out of range in while statement but I can't figure out how. 1 Answer

Black background when the ARCamera is started 2 Answers

how to make ball rolling forward while follows a curve track? 0 Answers

Unity-Android read file from device storage 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