x


Logic correction in function(cube click)

Hii, i'm trying to execute one function , but not able to complete it successfully, if anybody is there help me .

this is my function code

var clicked : boolean; //Will default to False

function OnMouseDown () {
  clicked = true; //Set this one to true
  Debug.Log("clicked");
  CheckCubes();
}


function CheckCubes() {
  var cubes = this.gameObject.FindGameObjectsWithTag("ClickObjects");
  for (i=0;i<cubes.length;i++) {

    if(clicked) {
     cubes[i].active=false;
    }

    if (!clicked) {
      break;    
    }

    if(cubes[i].active==false) { // its checking that all cubes are false
      clicked=true;
    }

    if(clicked==true) {
      Application.LoadLevel("level1");
    }
  }
}

when i'm try to execute it,

  1. if i'll click on any cube , it considers that all cubes are clicked, and directly going to next level. why is it so?
more ▼

asked Mar 21 '11 at 01:46 PM

pravin gate gravatar image

pravin gate
27 18 18 25

Do you want the player to click at the cubes, then deactivate the cube and when finished load the next level?

Mar 21 '11 at 02:07 PM efge

yes , when player will click on all cubes then and then only next level will be load

Mar 21 '11 at 02:14 PM pravin gate

i have tried to build logic in such a way that ....

for(i=0;i<cube.length;i++)

if i'll cliked on cube it will disable that cube

if i'll not click on any cube. then break...

then i checked that if all cubes are disable ,

if true, next level will appear.

Mar 21 '11 at 02:25 PM pravin gate
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You could use this script:
Send a ray from mouse position, check if object with tag "ClickObjects" was hit, deactivate this object, decrease a counter and if it is zero load the next level.

var cubeCount : int;

if(Input.GetMouseButtonDown(0)) {
  var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  var mousehit : RaycastHit;        
  if (Physics.Raycast (ray, mousehit) && (mousehit.collider.tag == "ClickObjects")) {
    mousehit.transform.gameObject.active = false;
    cubeCount --;
    if (cubeCount == 0)
      Application.LoadLevel("level1");
  }
}
more ▼

answered Mar 21 '11 at 02:28 PM

efge gravatar image

efge
5.1k 5 14 40

sure, i 'll just chk it.

Mar 21 '11 at 02:36 PM pravin gate

i used this code , but didnt happened anything.

is there any way in unity lik c, to check code by debugging? so we can get idea what is actully happening?

Mar 21 '11 at 02:47 PM pravin gate

Did you attach a BoxCollider to the cubes?

Mar 21 '11 at 03:02 PM efge

Hey , with the help of ur code , whenevr i'll click on cube , it is disappearing. its really good thing. but when i'll click on my last cube in prefab, it should have to load next level. only that thing is not working. i think we should need to chk out our count value.

do u know how can i chk that how many cubes(objects) are contained in my prefab currently??. becauz until count value goes to zero it will not load next level.

Mar 22 '11 at 05:00 AM pravin gate

Hii efge, as we r not declared count value, it takes it as -1,-2,-3....

and until our count value will not goes to zero it will not load next level.

when i tried to chk by Debug.log(cubecount),
count value will become -3 if & if only when i 'll click on cubes in sequence. otherwise it just goes up to -2 or -1 repectivly on clicking them randomly.

why this is happening? wht should we need to do for that?

Mar 22 '11 at 12:45 PM pravin gate
(comments are locked)
10|3000 characters needed characters left

Your final if (clicked == true) should be outside the 'for' loop. BTW You can use just 'if (clicked)'

more ▼

answered Mar 21 '11 at 03:28 PM

DaveA gravatar image

DaveA
26.8k 153 171 257

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1285
x232

asked: Mar 21 '11 at 01:46 PM

Seen: 714 times

Last Updated: Mar 22 '11 at 01:06 PM