- Home /
Make Unity know which target was hit first.
Ok, I'm stuck here.
I'm making a shooting range mini-game. I have a machine with 4 targets with numbers on them. I randomly generate the order of the numbers 1-4 and have the player shoot the targets in the order that I generate. (Make sense?)
What I need is a way to figure out in what order the player shoots the targets. I need to know which target he hit first, which target he hit second, etc. If there was a way I could make parameters I could send to another function that would tell the function what target was hit, and in which order it was hit, that'd be great. I just have no idea where to start.
Thanks ahead of time!!!
If you are using C#, take a look at events and delegates:
http://www.indiedb.com/games/coco-blast/tutorials/delegates-events-and-singletons-with-unity3d-c
http://www.youtube.com/watch?v=N2zdw$$anonymous$$IsXJs
Or you can find another game object and communicate directly:
http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
Ok, sorry I probably should have said this. I am using UnityScript.
$$anonymous$$y problem doesn't come with trying to access other scripts components, I need to know which target was hit first, which was hit second, etc. I need to know whether or not the player hit the targets in the right order.
Then I'm having trouble understanding where you are having the problem. For example: you have your tracking script attached to a game object somewhere in the project. All the targets get access to this tracking component. Using a singleton would make this easy, but you can use GameObject.Find() and GetComponent() to get access. Each target has an id as part of the script attached to the target. As each target registers a hit, it reports that id to the tracking script. The tracking script needs to know when a round starts, then it just has to see if the target is the next one in the order. When a round starts, it looks for target 1, if 1 is reported, it looks for 2. If a target comes in that is not one expected, it is out of order.
Answer by Kristian · May 16, 2013 at 02:03 PM
I assume that you want to check the order. A simple approach would be to give them identifying names i.e. "1", "2", "3" & "4". Then add them to a collected string on hit, and in the end check if the collected string is "1234". It's not the most optimal approach, but it should do the trick if I understood your issue correctly.