- Home /
OnTriggerEnterProblem
i have a script to call an RPC fuction when i collide with a flag in a ctf game. but when i collide with it an error comes up
NullReferenceException: Object reference not set to an instance of an object Flag.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/ObjectScripts/Flag.cs:28)
here's the script
NetworkManager manager;
public bool isRed;
void Start ()
{
manager = gameObject.GetComponent<NetworkManager> ();
if(isRed)
{
gameObject.GetComponentInChildren<InteractiveCloth>().renderer.material.color = Color.red;
gameObject.GetComponentInChildren<Light>().color = Color.red;
}
else
{
gameObject.GetComponentInChildren<InteractiveCloth>().renderer.material.color = new Color(0, 0.5f, 0);
gameObject.GetComponentInChildren<Light>().color = new Color(0, 0.5f, 0);
}
}
void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
manager.GetComponent<PhotonView>().RPC("FlagHolderChange", PhotonTargets.AllBuffered, other.gameObject, isRed);
}
}
I would suspect that one of your GetComponent Functions is not finding the component you want, and returning a null value. The line number in the error does not exist in this code, so it's hard to figure out which one.
then I suggest you break the call up into a few separate lines to find the problem: e.g.
PhotonView pv=manager.GetComponent<PhotonView>();
if(!pv) //checks for null
Debug.Log("PhotonView Get failed");
if this test fails- I'd double check your hierarchy & components in the editor.
Thanks man. I really should've done the tests before asking anything. The problem was the manager wasn't found but i fixed it with
manager = GameObject.FindObjectOfType ();
ins$$anonymous$$d of
manager = gameObject.GetComponent ();
Answer by jmgek · Dec 30, 2014 at 08:27 PM
Don't use Tags when you can, it is more expensive when you build out.
void OnTriggerEnter (Collider col)
{
if(col.gameObject.name == " ")
{
}
}
I don't understand what this example is trying to show, nor how it relates to the OP's question.
Your answer
Follow this Question
Related Questions
How to make game like Hoops Stack 1 Answer
Using the OnTriggerEnter ∁aring tags correctly. 1 Answer
AI Attack Not Working! W/Video 1 Answer
Checking trigger collision on other objects? 2 Answers
Help With Colliders 3 Answers