Sevyn
1
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!
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)