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 /
  • Help Room /
avatar image
0
Question by ncankeles · May 11, 2017 at 11:02 AM · c#nullreferenceexceptionnavmeshagenttarget

I'm going nuts for 2 days about NullReferenceException error please help!

Hello guys. My main character will pick an object "Pick ups" and Police will chase him(AIThirdpersonController). I couln't do it no matter what. Plase take a look at my codes. I'm trying to acces a function from another one. Thank you

Thats my PlayerController script on my main character(Wilson);

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.AI;
 using UnityStandardAssets.Characters.ThirdPerson;
 
 public class PlayerController : MonoBehaviour {
     public float speed;
     public Text countText;
     public Text winText;
     AICharacterControl PoliceRun;
     GameObject Winston;
     private GameObject police;
 
     public  int count;
     private Rigidbody rb;
     Transform target;
     private Vector3 place;
     string count1;
 
     // Use this for initialization
     void Start() {
         rb = GetComponent<Rigidbody>();
         count = 0;
         SetCountText();
         winText.text = "";
 
         police = GameObject.FindWithTag("Police");
         Winston = GameObject.FindWithTag("Winston");
         PoliceRun = GameObject.FindWithTag("Police").GetComponent<AICharacterControl>();
         Debug.Log(target.ToString());
 
         
     }
 
     // Update is called once per frame
     void Update() {
 
         
     }
 
     private void FixedUpdate()
     {
 
     }
 
     void OnTriggerEnter(Collider other) {
         if (other.gameObject.CompareTag("Pick Up")) ;
         {
             other.gameObject.SetActive(false);
             count = count + 1;
             SetTargetAI();
 
 
 
 
 
         }
 
     }
     void SetCountText() {
         count1 = countText.text;
         countText.text = "Collected=" + count.ToString();
         if (count >= 12 )
         {
             winText.text = "WIN!";
         }
 
     }
 
     void SetTargetAI()
     {
         PoliceRun.SetTarget(Winston.transform);
 
     }
     }
 
 
 

And Thats AICharacterControl (police) ;

 using System;
 using UnityEngine;
 using UnityStandardAssets.Characters.ThirdPerson
 namespace UnityStandardAssets.Characters.ThirdPerson
 {
     [RequireComponent(typeof(UnityEngine.AI.NavMeshAgent))]
     [RequireComponent(typeof(ThirdPersonCharacter))]
     public class AICharacterControl : MonoBehaviour
     {
         public UnityEngine.AI.NavMeshAgent agent { get; private set; }             // the navmesh agent required for the path finding
         public ThirdPersonCharacter character { get; private set; } // the character we are controlling
         public Transform target;                                    // target to aim for
 
         PlayerController pc;
         Transform WinstonT;
 
         private void Start()
         {
             // get the components on the object we need ( should not be null due to require component so no need to check )
             agent = GetComponentInChildren<UnityEngine.AI.NavMeshAgent>();
             character = GetComponent<ThirdPersonCharacter>();
 
             agent.updateRotation = false;
             agent.updatePosition = true;
 
             pc = GameObject.FindWithTag("Police").GetComponent<PlayerController>();
             WinstonT = GameObject.FindWithTag("Winston").transform;
 
         }
 
 
         private void Update()
         {
             if (target != null)
                 agent.SetDestination(target.position);
 
             if (agent.remainingDistance > agent.stoppingDistance)
                 character.Move(agent.desiredVelocity, false, false);
             else
                 character.Move(Vector3.zero, false, false);
         }
 
 
         public void SetTarget(Transform target)
         {
             this.target = target;
         }
     }
 }
 


I'm getting this error.

NullReferenceException: Object reference not set to an instance of an object PlayerController.SetTargetAI () (at Assets/Scripts/PlayerController.cs:74) PlayerController.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/PlayerController.cs:53

Comment
Add comment · Show 3
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 ncankeles · May 11, 2017 at 11:57 AM 0
Share

I've tried it, but no warning showed up :/

unitycoach

avatar image UnityCoach ncankeles · May 11, 2017 at 12:28 PM 0
Share

Did you try adding a breakpoint to see what is null?

avatar image phxvyper ncankeles · May 11, 2017 at 04:02 PM 0
Share

I moved your published answer to be a comment under the question as it does not fulfill the requirements to be an answer - it does not attempt to answer the question. This is a comment, not an answer.

Please ensure that, moving forward, your answers are indeed that - answers, not comments on the matter of the question.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by UnityCoach · May 11, 2017 at 11:24 AM

The only thing I see is that one the private members were not assigned. Try adding the following checks.

 police = GameObject.FindWithTag("Police");
 Winston = GameObject.FindWithTag("Winston");
 PoliceRun = GameObject.FindWithTag("Police").GetComponent<AICharacterControl>();
 if (!police)
     Debug.LogWarning ("police not found");
 if (!Winston)
     Debug.LogWarning ("Winston not found");
 if (!PoliceRun)
     Debug.LogWarning ("PoliceRun not found");
Comment
Add comment · Show 1 · 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 phxvyper · May 11, 2017 at 04:03 PM 0
Share

Also ensure that you're actually setting the Tag to "Winston" - not "winston" or any other tag. Case sensitivity is important.

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

348 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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

How do I organize a list of GameObjects (OrderByDescending?) based on the property of an attached component? 0 Answers

NullReferenceException: Object reference not set to an instance of an object Groundcheck.OnTriggerEnter2D (UnityEngine.Collider2D col) (at Assets/object/code/Groundcheck.cs:17) 0 Answers

Not sure why i'm getting NullReferenceException 0 Answers

NullReferenceException: Object reference not set to an instance of an object in Singleton class. 1 Answer

Weird Error With References 1 Answer


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