- Home /
How do i get attributes of another object?
For instance, i have the player variable in my enemy script:
player = GameObject.Find("Player2");
I want to know, if the boolean flag 'running' is true or false in the player, but the in the enemy script. How do i do that?
Thank you
Answer by skovacs1 · Sep 22, 2010 at 10:34 PM
There are 2 ways.
Use a static variable in your player script . This means that every instance of the player script will share this variable instance. If Player1 and Player2 both have this script attached, there will only ever be one of this variable that they are both using.
playerScript.js
static var running : boolean = false;
anyOtherScript.js
if(playerScript.running)
//do something
Use a public variable and get it from the instance of the script.
playerScript.js
var running : boolean = false; //public by default. You could specify with keyword public.
anyOtherScript.js
var player : GameObject = GameObject.FindWithTag("Player2"); //more efficient than Find
var script : playerScript = player.GetComponent(playerScript); //get script instance
if(script.running)
//do something
Your answer
![](https://koobas.hobune.stream/wayback/20220613073657im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Have script detect which collider 2 Answers
How to access a non static variable by another static variable? 1 Answer