unityengine.gameobject error

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?

Thnks,

  • How can you possibly use the variable clicked as both global boolean variable and local game object.

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");
      }
   }

yes u r rit Dinesh
i did it by mistake…

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");
      }
   }
}

why?

The variable clicked is not a member of the GameObject cubes, It’s just a member of the script… So
Change the line

Sorry about the above comment:

  • You can’t use such code

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.

This can be done by using

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 .

Because your logic is still same…

  • When a particular object is clicked… find it’s array index number " i "… then disable the particular object by

Finally this code worked…

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,

how should i achieve this?

if still need to explain , let me know.

529880--18717--$Untitled.jpg

What are you doing here actually…

if(clicked)
{

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;
}

Check here…

When coming into the following loop, the variable clicked is set to " True" right…

yes, ur logic is rit.

bt if i change according to ur lines :

var clicked : boolean; //Will default to False
var cnt=0;
function OnMouseDown ()
    {
      Debug.Log("clicked");
    CheckCubes();
    }

none cube is clickable.

i have also tried by setting value of var clicked to true.
but none cube is clickable aftr removing this value. "clicked = true; "

I’m not sure about this but try this…

if this isn’t working , you must find the particular object clicked by matching the co ordinates of mouse cursor and each object (Cube1,cube2).

Hello everybody, After long tried, i came to this level. Using this script i am able to go to next level aftr click on last cube in my scene.

But then also in some cases , it again falls.Anybody is having any idea what should i need to do it correct in this code.

var cubeCount : int;

function Update () {

if(Input.GetMouseButtonDown(0)) {

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

var mousehit : RaycastHit;        

if (Physics.Raycast (ray, mousehit)  (mousehit.collider.tag == "level3")) {

Debug.Log(cubeCount);
mousehit.transform.gameObject.active = false;
cubeCount --;
Debug.Log(cubeCount);

if (cubeCount == 0)
{
Debug.Log("chk");

Application.LoadLevel("Level3_hint");
}
}
}
}

I have attached 4 cubes for reference.

In that one can click on all cubes in different 16 ways. so how can i acheive success in all these 16 cases?

Any help will be helpful

Thanks,

531738--18769--$boxes.jpg

what do you mean by “it again falls”…

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.

have you initialized the cubeCount variable to 4?