- Home /
How do I count laps for AI horses in a horse racing game?
I have two C# scripts: AI - controls the AI horses, and LapTrigger - counts the laps for the player and the AI horses.
AI.cs
I have a private int lapCounter
and a public void incrementLap()
that simply increments the lap (well, it's supposed to...).
LapTrigger.cs
I have a method, OnTriggerEnter(Collider other)
, that checks to see if other.tag
is "player" or "AI". If it's the player, it just increments a playerLapCounter inside the LapTrigger script. If other.tag
is AI, then I would like to call AI.incrementLap()
but I have NO idea how to figure out which AI is was that triggered the LapTrigger.
I've tried creating an array of AI objects using FindObjectsWithTag()
, but I still don't know how to figure out which of those triggered the lap. I tried playing around with other
, other.GetComponent
, and MANY other things, but I haven't found anything that gives me anything more than a Null Reference Exception. I've been able to assign "horse numbers" to the AI horses in the Start()
method when the game itself starts, but I don't know how to access the horseNumber
property when the horse triggers the lap. For example, if the other.tag == "AI"
, then get the horseNumber
, and then increment AIhorseArray[horseNumber].lapCounter
(`horseNumber` isn't visible to the player so horseNumber = 0
is okay).
I even thought about creating an array in the LapTrigger script that just stores the AI horses as they cross the LapTrigger. The first horse that crosses becomes AIhorse[0]
and his lap counter is set to 1 (since he just completed a lap). However, I still have the same problem. The next time the horses come around (lap 2), how can I tell which AIhorse[?]
it is? Or even if it's a new horse or one that's already completed a lap?
I hope this makes sense and thanks to anyone who can help me - even if it's just a link to another post, tutorial, or anything that might help. If I had hair, I would have pulled it out already. I've been working on this for weeks!
Answer by getyour411 · Aug 25, 2013 at 10:45 PM
From OnTriggerEnter you get other, so using other.gameObject...(that game object's hierarchy) gives you access to the gameObject that hit the trigger therefore you don't need Find/Tag etc, you have the gameObject. If that solves your issue I'll change this to an answer.
A better example is here, expand the part about accessing another GameObject/Script from trigger/collider hit (part II)
Thanks, getyour411. It's still not working, but I think the advice and the awesome link you gave will get me there. After I type other.gameObject, I don't see any methods or variables outside of those local to the LapTrigger script.
I'm going through the code again to see if maybe I have some funky access levels in my AI script and I will definitely study the link you gave.
Thanks again for your help!
Post back here if you still need help with this; in your $$anonymous$$onoDevelop context "other" (as the reference to the trigger) could be anything, so maybe it can't show you methods, however that does not mean they can't be added by you via the script. For example :
void OnTriggerEnter (Collider collider) {
if(collider.gameObject.tag == "$$anonymous$$oney") {
int mp = collider.GetComponent<Q$$anonymous$$_$$anonymous$$oneyPickup>().myAmount;
I can tell it what component to get manually on another script (Q$$anonymous$$_$$anonymous$$oneyPickup) attached to a different GameObject.
That's it exactly! I actually figured it out reading the link you posted then came back here and saw your comment lol! Thanks a ton for your help!
I can get to the variables and methods now, so should I use a method to increment the lap ins$$anonymous$$d of accessing the variable directly?
Either sounds fine, it might be a little cleaner in terms of potential debugging if you call a method in the target class to increment just so you know where changes are co$$anonymous$$g from, but the hook reference other.horse....var+1 will also work.
Your answer
![](https://koobas.hobune.stream/wayback/20220613112952im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Null Reference Exception Error. Modified Stats. Need Help. 1 Answer
Null reference from object imported through method 1 Answer
I keep getting a null reference exception, can anyone help? 0 Answers
Null Reference Exception, what is it and why do I get it? 1 Answer
Camera Ray - NullReferenceException: Object reference not set to an instance of an object 0 Answers