- Home /
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.
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.
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).
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.
Your answer

Follow this Question
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