- Home /
Error in..."calling"(?) script
This is what happens when you don't code for a month. You forget really important things. So, I need to access this script from another script, but it's erroring out when the object with the below script hits an enemy object.
Null Reference Exception: Object Reference not set to an instance of an object EnemyDetectionScript.OnTriggerEnter (UnityEngine.Collider hit) (at.....line 12)
What is going on? I feel like I should know this... Thanks for helping on this question.
var playerScript : MainCharBehaviorCC;
function Start()
{
playerScript = GameObject.FindGameObjectWithTag("Player").GetComponent(MainCharBehaviorCC);
}
function OnTriggerEnter(hit : Collider)
{
if(hit.collider.tag == "enemy")
{
playerScript.test = true;
}
}
The player tagged and script present on him? And the enemy tag is correct? Collider set to 'Is Trigger'?
Perhaps try hit.collider.gameObject.tag if nothing else works.
(meat, you always seem so helpful :)
Player tagged and the script is attached. Yes, the collider is set to isTrigger. If I take out line 12 and put a Debug there, no error occurs. It has to be something with referencing the script!
(Thanks :))
You could access test directly with GetComponent. I guess, check test has accessible scope. It must be declared outside functions and not be nested.
I suppose FindWithTag may work better. Or try CompareTag. But I don't think FindGameObjectWithTag is actually in the docs. It actually has an 's' in it 'Objects' and returns an array.
I have to agree with meat5000 here. The problem is most likely the call to GameObject.FindGameObjectWithTag(). I can't find any documentation on it. I always use GameObject.FindWithTag() in situations like this.