Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sevyn · Aug 25, 2013 at 10:42 PM · null reference exceptionnull reference

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sevyn · Aug 26, 2013 at 11:14 PM 0
Share

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!

avatar image getyour411 · Aug 27, 2013 at 12:05 AM 0
Share

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.

avatar image Sevyn · Aug 27, 2013 at 12:30 AM 0
Share

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?

avatar image getyour411 · Aug 27, 2013 at 01:40 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges