- Home /
Checking detection of gameobject correction
hii, i'm trying to develop one script for detection of cubes.
Below is that script.
script.js
function OnMouseDown () { Debug.Log("clicked"); CheckCubes(); }
function CheckCubes() { var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition); var mousehit : RaycastHit;
if (Physics.Raycast (ray, mousehit) && (mousehit.collider.tag == "OLD_TRY")) { mousehit.transform.gameObject.active = false; } }
This script uses raycast concept, and if we click on gameobject ,gameobject disappears.
Now i just need to check that if all gameobjects are hitted(clicked), goes to next scene.
So anybody is having any idea or script, what should i can use to check all gameobjects are hitted?
Thanks,
Any suggestions will be helpful.
$$anonymous$$ore information regarding this:
I have taken n cubes , 1)First created a prefab,
2)then addes a custom tag and added it to prefab.
3)Then created this script and attached to this prefab
4)Then added all n cubes to that prefab.
4)so all cubes will takes properties of prefab.
Hope so it will be helpful.
Answer by Bunny83 · Mar 23, 2011 at 11:53 AM
Just a little hint: OnMouseDown is not called just when you press the mouse button. It's called when you click on the collider of a GameObject. It's called just on that GameObject so you don't need to raycast. The OnMouseDown event is created by Unity and already uses a raycast internally to find the hitted GO.
That would do the same if every of your GameObjects that should be hitted have this script attached.
function OnMouseDown ()
{
Debug.Log("clicked");
gameObject.active = false;
}
In your case it would be the simplest thing to check right after you clicked on a GameObject. Just search for all gameobjects that have this script attached (name: script.js)
function OnMouseDown () { Debug.Log("clicked"); gameObject.active = false;
// check if all objects are disabled
var allObjects : script[] = Object.FindObjectsOfType(script) as script[];
var allDeactivated = true;
for (var current : script in allObjects)
{
if (current.gameObject.active)
allDeactivated = false;
}
if (allDeactivated)
{
// here we know all are disabled, do what you want
// eg. Application.LoadLevel("NextLevelName");
}
}
hey i'm getting one problem, still not able to go to next level after last click on gameobject. First i was created a script wit name "script.js" function On$$anonymous$$ouseDown () { Debug.Log("clicked"); gameObject.active = false; } ////////////////////////////////////////
and attached that script to all n cubes
/////////////////////////////////////// and then created ur script for checking disability of objects. Is there any mistake happened by me? so why it didnt working?
Answer by efge · Mar 23, 2011 at 12:02 PM
I thought this question was already answered here?!
You should set cubeCount to a valid number in the Inspector or by counting all GameObjects with tag = "ClickObjects".
yes sir, i was tried on that way also, but alerady as we have discussed , it is not checking all the cases.(Any sequence of clicking of cube ).
ok , just tel me for confirmation, i was changing the cubecount values of prefab and also, all cubes. is it rit way?