Hi,
I am trying to talk to another function in a different script and I am getting an error. This function is in a script called fireScript and is attached to a gameObject tagged “Player”. This is what I have in my fire scipt-
function PowerUp(){
print("PowerUp");
}
Then, I try to use this in a separate script to execute the function-
function OnTriggerEnter ( Col : Collider ) {
if(Col.gameObject.tag == "Player"){
gameObject.Find("Player").GetComponent(fireScript).PowerUp();
I get an error that says "Powerup is not a member of UnityEngine.Component.
Any Ideas?
Hi
In your script try this (below):
function OnTriggerEnter ( Col : Collider )
{
if(Col.gameObject.tag == "Player")
{
var scriptFire : fireScript;
scriptFire = gameObject.Find("Player").GetComponent(fireScript) as fireScript;
scriptFire.PowerUp();
}
}
If this will occur many times then cache the fireScript component for reuse (below)
var scriptFire: fireScript;
function Start()
{
// find the fireScript component attached to the Player gameobject and cache it for later
scriptFire = gameObject.Find("Player").GetComponent(fireScript) as fireScript;
}
function OnTriggerEnter ( Col : Collider )
{
if(Col.gameObject.tag == "Player")
{
scriptFire.PowerUp();
}
}
Regarding the caching part you can skip the Find method in the Start if you drag and drop the player object onto the scriptFire var in the editor at design time (below):
var scriptFire: fireScript; // MAKE SURE YOU DRAG THE PLAYER GAMEOBJECT TO THIS VARIABLE IN THE EDITOR
function OnTriggerEnter ( Col : Collider )
{
if(Col.gameObject.tag == "Player")
{
scriptFire.PowerUp();
}
}
Cheers
Thanks for your help and the quick reply!