- Home /
Accessing and using another objects script variable
I am trying to make a life counter. I have used the premade 3rd person controller that comes with unity and named it "PersonController".
I have attached the following script (called LifeCounterPlayer) to it:
static var lifeCounter : int = 3;
function OnCollisionEnter (otherObject : Collision) { if (otherObject.gameObject.tag == "enemy"){ lifeCounter --; } }
I have then created a GuiText called "GuiLifeCounter". I am not sure how to gain access to the variable "lifeCounter" and display it in this guiText. I have tried the following:
function Update () { var playersLife = PersonController.GetComponent("lifeCounter"); guiText.text = "Players Life = " + playersLife; }
Next I will try and have a guiTexture that displays the lives. eg - one heart in the top left corner for each life the player has. If someone has seen a tutorial video or some other type of tutorial that will help, I would really apprieciate it.
Thanks for the help.
Answer by ConfinedDarkness · Jul 26, 2011 at 12:05 AM
I am just going to give my own example here.
Script1:
var aNumber : int = 0;
Script2:
var otherObject : GameObject;
function Update () {
otherObject.GetComponent("Script1").aNumber += 1;
}
I hope this helped you!
Your answer
Follow this Question
Related Questions
Difficulties accessing other components 4 Answers
Accessing a variable within a function.... GetComponent() 2 Answers
Accessing other gameobject's script variables : why doesn't this work? 2 Answers
Can't understand generic getComponent for Js 1 Answer
Inheriting variables from common parent class on two different gameobjects 1 Answer