Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Eco-Editor · Mar 23, 2017 at 10:38 PM · collidernavmeshagenttargetfunctionswaypoints

Calling a function from colliding with an Object

Hello everybody,

My goal is to: 1. have a "player" enter a collider 2. the collider has a script on it, that once the player enters it, it's calling a function from a Patrol script on the "avatar". 3. the "avatar" now needs to go to a new destination of the "player" (deviate from the patrol points). 4. the "avatar" needs to stand by the user.

This is the fun. on the collider:

  void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "Player")
             print("a player in the colider");     
         {
             foreach (GameObject avatar in avatars)  
             {
                 if (Vector3.Distance(other.gameObject.transform.position, avatar.transform.position) <= minDistance)
                 {
                     PS = avatar.GetComponent<Patrol>();
                     if (PS != null) 
                     {
 
                         PS.GoToSpecificPoint(other.gameObject.transform);

}

here's a code in the "avatar":

  public void GoToSpecificPoint(Transform targetLocation)
     {
         agent.destination = targetLocation.position;
          if (Vector3.Distance (agent.destination, targetLocation.position)<= 0.1f)
         {
             agent.Stop();
             
         }

For some reason it's not working, and on one of the projects it gives me an error: NullReferenceException: Object reference not set to an instance of an object Patrol.GoToSpecificPoint (UnityEngine.Transform targetLocation) (at Assets/Scripts/Patrol.cs:84) ShelfEvent.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/ShelfEvent.cs:38)

Which seems to be the problem, that the agent.destination is set to "targetLocation" but it seems to be not enough.

Thanks to any possible ideas.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SohailBukhari · Mar 24, 2017 at 07:07 AM

@username Best way of Doing this is to use Events.You Don't need to take references or composition in your scripts.

 public class UnityFunction
 {
     public UnityEvent Collidinginfo;
 
     private void Start()
     {
         if (Collidinginfo != null)
             Collidinginfo.AddListener(delegate
             {
                 //do what you want
             });
     }
 
     private void OnTriggerEnter(Collider col)
     {
         //then inform to All scripts or function that collide with points
         if (Collidinginfo != null) Collidinginfo.Invoke();
     }
 }
Comment
Add comment · 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
1

Answer by Eco-Editor · Mar 26, 2017 at 09:19 AM

Thanx Nyonge,

What I actually did to solve this is I put the calling function on my Player, so when the player is colliding with the "collider", the function is called. I reversed the logic. like with Muhammad and the mountain :) Anyway I wish to have a good solution for my first approach as well... I'll try what you said. But for now I guess this is solved.

Comment
Add comment · 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
0

Answer by nyonge · Mar 23, 2017 at 11:28 PM

NullReferenceException means one of the variables you're attempting to access is null. The two variables you first use in GoToSpecificPoint are "agent" and "targetLocation". targetLocation is probably not null, because you're calling it from an OnTriggerEnter, so it's most likely "agent" that's null.

Putting a check for

 if (agent != null) {

will prevent the error. Make sure agent is properly being assigned on the script before using it. You can also throw in

 if (agent == null) {
 Debug.LogError("Error, agent is null, can't GoToSpecificPoint", gameObject);
 } else {
 //move to destination
 }

to help debug when the issue happens. Good luck!

Comment
Add comment · 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
0

Answer by GamingOink · Mar 24, 2017 at 07:55 AM

You could make an "if statement" that will call a function by functionName() when the if statement it true.

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

Collider with NavmeshAgent: Is it static? 0 Answers

NavMeshAgent issue 2 Answers

Having trouble with enemy AI and Collider 0 Answers

Navmesh sometimes falling through world when enabled 0 Answers

HELP! i want to write to look at the target and give a force to follow 3 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