How to get component in unity code example
Example 1: how to get component in unity c#
GetComponent<Rigidbody>(); //used to find component on character (rigid body can be changed)
GameObject.FindGameObjectWithTag("player"); //finds any game object in the scene with this tag
Example 2: getcomponent
ur so stoopid u dum its eeeeeeez to use getcomponent jkust doo
Getcomponent<*A component to get*>().*the thing you need in the component* *100/= true/= false/ color.red
Example 3: unity get component
using UnityEngine;
public class TryGetComponentExample : MonoBehaviour
{
void Start()
{
// Since Unity 2019.2 you can use TryGetComponent to check
// if an object has a component, it will not allocate GC in
// the editor if the object doesn't have one.
if (TryGetComponent(out Rigidbody rigidFound))
{
// Deactivate rigidbody
rigidFound.enabled = false;
}
// For versions below 2019.2 you can do it this way:
// Create a variable
Rigidbody rigidFound = GetComponent<Rigidbody>();
// If the 'Rigidbody' exist in the gameobject
if(rigidFound != null)
{
// Deactivate rigidbody
rigidFound.enabled = false;
}
}
}
Example 4: unity get component
public GameObject Var; //only if the component you want is on a different gameObject
void Start(){
Var.GetComponent<Component>(); //just GetComponent<Component>(); if the component you want is on the same object as script
}