unity Player.getcomponet<Move2S> code example
Example 1: getcomponent c#
public class Enemy : MonoBehaviour{
public int health = 10;
public void saySomething(){
Debug.Log("Hi, i am an enemy.");
}
}
public class Player : MonoBehaviour{
GameObject enemyGameObject;
void Start(){
Enemy enemyScript = enemyGameObject.GetComponent<Enemy>();
Debug.Log("Enemy health is:"+ enemyScript.health)
enemyScript.saySomething();
}
}
Example 2: findobject getcomponent
GameObject.Find("Name of the object you want to access").GetComponent<Name of the Component (Transform,Script,RigidBody,etc..)();
An Example (easy to understand):
GameObject Player = GameObject.Find("Player");
PlayerController PlayerControllerScript = Player.GetComponent<PlayerController>();
PlayerControllerScript.run = true;
Another Example:
GameObject.Find("Player").GetComponent<PlayerController>().run = true;