- Home /
Problems accessing a function
Okay, so I know people ask this a lot but this is a specific question (I already basically know how to do it)
I'm trying to access a script inside a particular object and call a function in it, I essentially just copied the code from unity script reference that says how to do that.
var ConPlat = GameObject.FindWithTag("AntiCore");
ConPlat.GetComponent(CollideCounter).Damage(MissDamage);
But it keeps saying that on that second line that "object reference not set to an instance of an object". I know it's not talking about MissDamage because that's an int, and there is a script called CollideCounter, and the object that has that script does have a "Core" tag. So I'm confused.
Try splitting it up, like so, and putting Debug.Log inbetween every step. That way, it'll tell you where it's going wrong!
var ConPlat : GameObject = GameObject.FindWithTag("AntiCore");
Debug.Log(ConPlat);
var collideCount : CollideCounter = ConPlat.GetComponent(CollideCounter);
Debug.Log(ConPlat);
collideCount.Damage($$anonymous$$issDamage);
Answer by Catlard · Oct 02, 2011 at 06:03 AM
I see two possible places for an error:
1) You've said it had a "Core" tag, but does it have an "AntiCore" tag like your code asks for?
2) GameObject.FindWithTag returns ONE gameobject, but I believe it's a random object with that tag. Maybe it's not the gameobject with the "AntiCore" tag that you're looking for--and maybe the object that it's finding doesn't have the CollideCounter script on it. You know?
Yes, it's the 'first' object with that tag... whatever that happens to mean at the time.
If there's a lot of objects with that tag, do you want to iterate through them? Apply the same logic to every one?
It has a Core and AntiCore tag, and there's only one object for each tag. Each object has the CollideCounter script on it, as I said. Any other ideas?
Have you tried what I suggested in my comment? It could shed some light on your problem.