hii, this may be a silly question , but need to be cleared. see this code
check.js
var clicked : boolean; //Will default to False
function OnMouseDown ()
{
this.clicked = true; //Set this one to true
CheckCubes();
}
function CheckCubes()
{
var cubes = this.gameObject.FindGameObjectsWithTag("ClickObjects");
for( var i=0; i < cubes.length; i++ )
{
if( !cubes[i].clicked ) //Break out if ANY cubes are not clicked
{
break;
}
if( i == cubes.length-1 ) //If we're on the last one, and made it this far, it means all are clicked
{
Application.LoadLevel("level1");
}
}
}
when i’m trying to compile it , giving me this error:
‘clicked’ is not a member of ‘UnityEngine.GameObject’.
Actually i’m using variable clicked as a boolean value for checking purpose.Then why this error is coming?
Because FindGameObjectsWithTag returns gameobjects. Your variable clicked is defined in your component attached to it. Use GetComponent(“check”) if your script is named check.js to get the correct component.
Think about storing the results of this getcomponent calls somewhere if you are using them several times.
for( var i=0; i < cubes.length; i++ )
{
var myScript = cubes[i].GetComponent("check");
if( !myScript.clicked ) //Break out if ANY cubes are not clicked
{
break;
}
if( i == cubes.length-1 ) //If we're on the last one, and made it this far, it means all are clicked
{
Application.LoadLevel("level1");
}
}
now i have changed script according to u. but still that error is remained.
var clicked : boolean; //Will default to False
function OnMouseDown ()
{
clicked = true; //Set this one to true
CheckCubes();
}
function CheckCubes()
{
var cubes = this.gameObject.FindGameObjectsWithTag("ClickObjects");
for( var i=0; i < cubes.length; i++ )
{
if( !cubes[i].clicked ) //Break out if ANY cubes are not clicked
{
break;
}
if( i == cubes.length-1 ) //If we're on the last one, and made it this far, it means all are clicked
{
Application.LoadLevel("level1");
}
}
}
but using this code i’m able to click on objects and its working, but it doesnot use for loop.
It is directly going last if statement.
???
if i m clicking on any cube it is directly going to next level1.
var clicked : boolean; //Will default to False
function OnMouseDown ()
{
clicked = true; //Set this one to true
CheckCubes();
}
function CheckCubes()
{
var cubes = this.gameObject.FindGameObjectsWithTag("ClickObjects");
for( var i=0; i < cubes.length; i++ )
{
if( !clicked ) //Break out if ANY cubes are not clicked
{
break;
}
if( i == cubes.length-1 ) //If we're on the last one, and made it this far, it means all are clicked
{
Application.LoadLevel("level1");
}
}
}
Do you want the Cube object to appear even after it is clicked… If not just disable the game object when it is clicked. Now you can move to next level when all the cube objects are disable.
yes i would lik to disable them after click on them.
but still main problem is not solved.
is there any problem wit our for loop ?
a have been added cubes*.active = false; in simple way.* ```
*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( var i=0; i < cubes.length; i++ )
{
if (clicked)
{
cubes[i].active = false;
}
if( !clicked ) //Break out if ANY cubes are not clicked
{
break;
}
if( i == cubes.length-1 ) //If we’re on the last one, and made it this far, it means all are clicked
{
Application.LoadLevel(“level1”);
}
}
}* ``` i have taken a new prefab and added my 2 cubes into it.And then created a custom tag with name ‘ClickObjects’ and added to prefab. so by default it is applied to other 2 children cubes . And then i’m attached this script to prefab. so it is also indirectly attached to 2 cubes .
var clicked : boolean; //Will default to False
var cnt=0;
function OnMouseDown ()
{
clicked = true; //Set this one to true
Debug.Log("clicked");
CheckCubes();
}
function CheckCubes()
{
var cubes = this.gameObject.FindGameObjectsWithTag("ClickObject");
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;
}
for (i=0;i<cubes.length;i++){
if(cubes[i].active==false)
cnt++;
}
}
if(cnt==cubes.length){
Application.LoadLevel("level1"); //go to next level
}
}
But still this code is doing one big mistake.
i’ll just explain wit my current scene just look at below my image scene.
there are 2 cubes in my prefab, with custom tag. when i’m clicking with sequence cube 1 to cube2, then nxt level is appeared after click on cube2.
** But if i’ll first click on cube 2 instead of cube1 , cube1 is disappearing, why is it so?
becauz if i’m click on cube 2 the ,it should be disappeared ,instead of cube1,
cubes*.active=false; // clicked cube will disable. why this line is considering detection of cubes in only sequence manner.* } if (!clicked) { break; }
if(cubes*.active==false)// this loop is not doing anything. if we remove this , still problem remains.* { clicked=true; }
see i have 4 objects here as above shown in image. Now i can click on these objects in different 16 ways(consider probability). so these all cases should becomes true, but my problem is that in some cases they r not working, instead before clicking of all cubes they r going to next level.
if u want to chk at ur pc, u can chk. and try to check by setting cubeCount to a valid number in the Inspector.