1ur so stoopid u dum its eeeeeeez to use getcomponent jkust doo
2
3Getcomponent<*A component to get*>().*the thing you need in the component* *100/= true/= false/ color.red
1 //First script
2public class Enemy : MonoBehaviour{
3 public int health = 10;
4 public void saySomething(){
5 Debug.Log("Hi, i am an enemy.");
6 }
7}
8//Second script
9//There is one thing though,
10//if the script is located on another gameobject,
11//which will be most likely, you have to somehow
12//find that gameobject first. There are many ways of
13//doing it, via raycast, collision,
14//trigger, find by tag, find by gameobject etc.
15//But i will use the simplest way to understand,
16//that is declaring public GameObject in player script,
17//which contains Enemy, and later you just drag and drop the
18//gameobject in the inspector.
19public class Player : MonoBehaviour{
20 //Drag and drop gameobject with script Enemy in inspector
21 GameObject enemyGameObject;
22 void Start(){
23 //We get Enemy component - script
24 //from public gameobject which contains this script
25 Enemy enemyScript = enemyGameObject.GetComponent<Enemy>();
26 //This will debug Enemy health is: 10
27 Debug.Log("Enemy health is:"+ enemyScript.health)
28 //This will debug "Hi, i am an enemy."
29 enemyScript.saySomething();
30 }
31}
1using UnityEngine;
2
3public class TryGetComponentExample : MonoBehaviour
4{
5 void Start()
6 {
7 // Since Unity 2019.2 you can use TryGetComponent to check
8 // if an object has a component, it will not allocate GC in
9 // the editor if the object doesn't have one.
10 if (TryGetComponent(out Rigidbody rigidFound))
11 {
12 // Deactivate rigidbody
13 rigidFound.enabled = false;
14 }
15
16 // For versions below 2019.2 you can do it this way:
17 // Create a variable
18 Rigidbody rigidFound = GetComponent<Rigidbody>();
19
20 // If the 'Rigidbody' exist in the gameobject
21 if(rigidFound != null)
22 {
23 // Deactivate rigidbody
24 rigidFound.enabled = false;
25 }
26 }
27}
1public Gameobject a; //this allows you to drag an object into a slot in the unity software
2private transform b; //this is a variable that can only be assigned and altered in script
3
4b = a.GetComponent<Transform>(); //this finds the transform component of 'a' and assigns that to 'b'