- Home /
How do I pass along which script triggered a function in trans script functions?
I have several instantiated prefabs(let's call them zerglings) that all have scripts attached to them, and one script(let's call it overlord) on an off camera object that is supposed to keep track of one boolean(let's call it hero) in all the zerglings(only one of them is allowed to have hero set at a time). As of now, overlord attempts this by keeping the GetComponent(zergling)
property in a variable. It gets the property passed to it in the parameters of a function that also sets hero to be true. Ideally, whenever a zergling sets hero to be true, overlord tells the zergling that had hero set to true, to turn hero to false.
the code looks a little like this:
function NewHero (WhichLing) {
if (HeroLing != WhichLing) {
HeroLing.GetComponent(Zergling).HeroOff();
HeroLing = WhichLing;
}
}
for overlord, and this:
function OnMouseDown () { overlord.gameObject.GetComponent(OverLord).NewHero(GetComponent(Zergling)); Hero = true; }
function HeroOff () { Hero = false; }
for the zergling.
With all the extra code commented out(so that there shouldn't be any interference) and monitoring the variables through print statements and the inspector pane, it produces shaky results. Sometimes it fails from the getgo, sometimes it works once then fails, and others it works for a while then stops working and only spits errors at me even when the variables look like what I think they should look like.
p.s. I think I might of lost/changed something when I simplified my code, but I think that accurately reflects what I have.
Answer by Bunny83 · Jan 15, 2011 at 05:53 AM
What kind of errors do you get? Can you post them? And btw. what is "overlord" in your Zergling script? A variable? Where is it defined? Is it initialized proberly?
If you really got only one OverLord script you should use a singleton pattern.
In your OverLord script:
static var instance : OverLord;
function Start() { instance = this; }
In your Zergling script you can do:
OverLord.instance.NewHero(this);