Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by toomasb · Jul 20, 2015 at 08:10 AM · gameobjectfunctionmissingreferenceexception

GameObject reference lost between functions

My gameobject reference gets lost between functions. The gameobject reference comes from a collider with a separate OnTriggerEnter script. That script works fine, and the info reaches the PlayerWarning function. After that, the target variable gets set back to null. I also tried putting the part that needs the reference in the PlayerWarning function, but then I got the error, that the NavMeshAgent was missing a reference to the object. The NPC still moved with the NavMeshAgent but it didn't register it in the function.

 using UnityEngine;
 using System.Collections;
 
 public class AIBasic : MonoBehaviour {
 
     public bool hostile;
     public bool shy;
 
     private Animation anim;
     private NavMeshAgent agent;
     private Vector3 destination;
     private Vector3 position;
     private float distToTar;
 
     [HideInInspector]
     public GameObject target = null;
 
     // Use this for initialization
     void Awake ()
     {
         anim = GetComponent<Animation>();
         agent = GetComponent<NavMeshAgent>();
         position = transform.position;
         InvokeRepeating ("NewTargetStandard", 1f, 4f);
         target = null;
     }
     
     // Update is called once per frame
     void Update ()
     {
         if (agent.remainingDistance <= agent.stoppingDistance)
         {
             if (agent.hasPath || agent.velocity.sqrMagnitude == 0f)
             {
                 if (!target)
                     anim.Play("Idle");
             }
         }
 
         if (hostile)
         {
             if (target)
             {
                 Debug.Log (target.ToString());
                 distToTar = (Vector3.Distance(transform.position, target.transform.position));
                 CancelInvoke();
                 destination = target.transform.position;
                 agent.destination = destination;
                 if (distToTar >= 1.5f)
                     anim.Play("Run");
             }
         }
         else if (shy)
         {
             //hide from player until attacked
         }
 
     }
 
     void NewTargetStandard ()
     {
         destination = new Vector3 (Random.Range(position.x - 13f, position.x + 13f), 0f, Random.Range(position.z - 13f, position.z + 13f));
         agent.destination = destination;
         anim.Play ("Walk");
     }
 
     public void PlayerWarning (GameObject player)
     {
         target = player;
     }
 
     public void StartUpProtocol (Vector3 pos)
     {
         position = pos;
     }
 }



EDIT: Found a workaround to the problem by giving the NPC a collider, with which to detect the player inside the same script. It works and all, but I'll keep the problem here open, because someone else might need this info or something.

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 Torigas · Jul 20, 2015 at 08:41 AM 0
Share

Can you post the part of the code that invokes the "PlayerWarning" method? Currently it looks like you're using the "target" variable and if Playerwarning gets a null object passed it sets "target" to null - hence everything which uses "target" gets null references.

avatar image toomasb · Jul 20, 2015 at 12:17 PM 0
Share

I already worked around the problem by adding a collider to the AI to detect the player, ins$$anonymous$$d of the room (which I intended). I sadly deleted the old script referencing it but I believe the part in question was something like this:

 public GameObject inhabitant1;
 
 //Here I instantiated the enemy NPC under the name inhabitant. The code is annoyingly long and I'm too lazy to re-write it here.
 
 void OnTriggerEnter (Collider other)
 {
     if (other.gameObject.tag == "Player")
     {
     inhabitant.GetComponent<AIBasic>().PlayerWarning (other.gameObject);
     }
 }


Like I said, this part works, as was proven by extensive debugging. The gameObject reference gets from this code to the AIBasic PlayerWarning function. But it returns null when called anywhere outside this function.

And, before you ask, yes, the AI was properly instantiated, no, it didn't have any errors regarding the instance being named Name (Clone).

avatar image toomasb · Jul 20, 2015 at 12:26 PM 0
Share

Also might be important: why I'm setting the target variable to null in Awake - for some reason the collider detected OnTriggerEnter from miles away when I start the program.

However it still detects the collision when the player really enters the collider.

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

C# UI component overflaping another gameobject function 0 Answers

[Editor Scripting] Call Function in other GameObjects Editor Script? 0 Answers

Finding the angle between two GameObjects 1 Answer

Cloned Prefab 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