Debug.Log() function use

hii,everybody, it may be simple basic question but really imp to understand.

i have created a prefab, also created custom tag for it. and attached '1' cube to it.

now i tried to written a simple raycast component related script to detect that cube after hit as follows.

if(Input.GetMouseButtonDown(0)) {

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

var mousehit : RaycastHit;

if (Physics.Raycast (ray, mousehit) && (mousehit.collider.tag == "try03")) // try03 is custom tag { mousehit.transform.gameObject.active = false;

Debug.Log(collider.tag.Length); }

}

now see that last line

Debug.Log(collider.tag.Length);

from this line i will get collider tag length, means if i am attached only one cube to prefab, means only

one tag is is there, and that Debug function line should give me value equal to '1'. but it is giving me

value 5 why?

Am i rit ? or this gives another value?

Any help will be helpful.

Thanks,

2 Answers

2

collider.tag.Length returns the length of your tags name.

So it is returning '5' because your tag is called 'try03', which is 5 characters in length.

To see how many cubes you have hit, you can add a simple variable in your code and increment it each time you hit a cube.

var mousehit : RaycastHit;

var numCubesHit = 0; 

    if (Physics.Raycast (ray, mousehit) && (mousehit.collider.tag == "try03")) 
    { 
      mousehit.transform.gameObject.active = false;
      numCubesHit++; // Increase the number of cubes hit here by 1 each time
      Debug.Log(numCubesHit); 
    }
}

ohh.. thanks. is there any other method to count , that how many cubes i u have hitted or clicked. Means if there r 2 cubes in my scene, above script i can apply to it. but how should i will know that how many cubes are clicked ?

–

so is there any way to use raycast, or something else to count how many cubes u have been hitted.?

–

I have changed the code to show you how to keep track of the number of cubes that have been hit.

–

yes ur logic is absolutly correct. i was also tried in this way. But i am getting one problem here. suppose i have taken 2 cubes in scene, and when i'm hitting 2 cubes after one another, i'm getting only one return value , i should need to get return value '2' using Debug.Log(numCubesHit). I think this is the main problem, that why i am not able to move to next level. Why is it so?

–

Where is your script attached to? Don't attach the script to the cube. Create an empty GameObject called GameManager. And attach your script to that. Then it should count correctly. Le tme knowif that works.

–

You are getting the length of the tag's string. I bet the name of that tag has 5 characters :)

A game-object can have only have one tag. The tag property is a string, not a string array.