find component unity3d code example
Example 1: unity get component
using UnityEngine;
public class TryGetComponentExample : MonoBehaviour
{
void Start()
{
if (TryGetComponent(out Rigidbody rigidFound))
{
rigidFound.enabled = false;
}
Rigidbody rigidFound = GetComponent<Rigidbody>();
if(rigidFound != null)
{
rigidFound.enabled = false;
}
}
}
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;