0

I have created two scripts one is scrpt_Enemy and other is scrpt_GameManager.

I want to call SubtractLive() function (exist on scrpt_GameManager) from scrpt_Enemy. But it gives error I don't know why. The error is 'SubtractLive' is not a member of 'UnityEngine.Component'.

scrpt_Enemy:

var gameManager : GameObject;
gameManager.GetComponent("scrpt_GameManager").SubtractLive();

scrpt_GameManager:

var lives : int =3;

function SubtractLive(){
    lives -= lives;
}

1 Answer 1

1

Problem

You were telling Unity that gameManager was of type GameObject. Therefore Unity was freaking out because it doesn't have SubtractLive() under GameObject.

Solution

You can get the correct reference like this:

Create the variable then tell Unity what the type it is.

var gameManager : scrpt_GameManager;

Find the GameObject which has the script attach to it.

gameManager = GameObject.Find("GameManagerGameObject").GetComponent("scrpt_GameManager");

Note that "GameManagerGameObject" is the name of the GameManager in the scene.

Now that you have the reference to the script just call the function like this:

gameManager.SubtractLive();
Sign up to request clarification or add additional context in comments.

2 Comments

Still Saying SubtractLive(); is not a member of 'UnityEngine.Component'.
I forgot something. You need this line: var gameManager : scrpt_GameManager; I have also included it in the answer above.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.