unity get component
C#
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;
}
}
}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
}using UnityEngine;public class GetComponentExample : MonoBehaviour
{
void Start()
{
HingeJoint hinge = gameObject.GetComponent(typeof(HingeJoint)) as HingeJoint; if (hinge != null)
hinge.useSpring = false;
}
}
Also in C#:
- Title
- unity random float
- Category
- C#
- Title
- how to get length of okobjectresult c#
- Category
- C#
- Title
- calculate impact damage + unity
- Category
- C#
- Title
- unity cycle children
- Category
- C#
- Title
- unity load scene
- Category
- C#
- Title
- how to get array of children transform
- Category
- C#
- Title
- c# 2 timespan return yesterday
- Category
- C#
- Title
- count the number of notes in a given amount c#
- Category
- C#
- Title
- c# image to byte array
- Category
- C#
- Title
- how to declare 2d array in c#
- Category
- C#
- Title
- vb.net tostring numeric format string
- Category
- C#
- Title
- C# .net core convert string to enum
- Category
- C#
- Title
- how to get value from object in c#
- Category
- C#
- Title
- how to add a gameobject
- Category
- C#
- Title
- isprime c#
- Category
- C#
- Title
- convert array object to int[] c#
- Category
- C#
- Title
- how to make a string a list of characters c#
- Category
- C#
- Title
- how to raycast unit
- Category
- C#
- Title
- unity rigidbody constraints
- Category
- C#
- Title
- unity get data from firebase
- Category
- C#
- Title
- how to turn a string in a char list c#
- Category
- C#
- Title
- drag object unity 2d
- Category
- C#
- Title
- Unity how to put IEnumerator in update and loop once with yeild return new waitforseconds
- Category
- C#
- Title
- c# move with arrow keys
- Category
- C#
- Title
- how to store some variables on the device in unity
- Category
- C#
- Title
- generate a dropdown list from array data using razor .net mvc
- Category
- C#
- Title
- get user directory of file in c#
- Category
- C#
- Title
- automapper c# initialize error
- Category
- C#
- Title
- c# convert dictionary to anonymous object
- Category
- C#
- Title
- c# return two variables of different types
- Category
- C#
- Title
- decalre an int list mvc
- Category
- C#
- Title
- get enum value c#
- Category
- C#
- Title
- find month number from date C#
- Category
- C#
- Title
- firefoxoptions setpreference to trust certificates
- Category
- C#
- Title
- unity how to rotate something to point to something else
- Category
- C#
- Title
- unity hide mouse first person
- Category
- C#
- Title
- No context type was found in the assembly
- Category
- C#
- Title
- what is c# used for
- Category
- C#
- Title
- http post request login example asp.net c#
- Category
- C#
- Title
- compose graphql query string in c#
- Category
- C#
- Title
- .sh script: check if file exist
- Category
- C#
- Title
- convert string to decimal c#
- Category
- C#
- Title
- unity instantiate
- Category
- C#
- Title
- and unity
- Category
- C#
- Title
- external font family uwp c#
- Category
- C#
- Title
- html hidden text
- Category
- C#
- Title
- unity vector3 smoothdamp not reaching target
- Category
- C#
- Title
- how to create a variable in C#
- Category
- C#
- Title
- snake game
- Category
- C#
- Title
- c# get all inherited classes of a class
- Category
- C#
- Title
- create expression func c# for use in where clause
- Category
- C#
- Title
- check if current time is in the morning c#
- Category
- C#
- Title
- how to clear console through script unity
- Category
- C#
- Title
- mysql executeScalar only if successful
- Category
- C#
- Title
- c sharp comments
- Category
- C#
- Title
- c# listview
- Category
- C#
- Title
- c# replace foreach with lambda
- Category
- C#
- Title
- asp.net get query string parameter
- Category
- C#
- Title
- c# exit program
- Category
- C#
- Title
- c# duplicate object instance
- Category
- C#
- Title
- unity get max occurrence in list
- Category
- C#
- Title
- c# size of enum
- Category
- C#
- Title
- unity up arrow input
- Category
- C#
- Title
- create object in c#
- Category
- C#
- Title
- how to do a messagebox in c#
- Category
- C#
- Title
- condition when a animation finishes in unity
- Category
- C#
- Title
- office open xml check if row is empty
- Category
- C#
- Title
- c# LCP
- Category
- C#
- Title
- C# save pdf stream to file
- Category
- C#
- Title
- modificare una strinfa in c#
- Category
- C#
- Title
- order by C#
- Category
- C#
- Title
- page parent wpf
- Category
- C#
- Title
- c# convert to nullable datetime
- Category
- C#
- Title
- convert string to datetime c#
- Category
- C#
- Title
- unity how to see what scen you are in
- Category
- C#
- Title
- appsettings in console application c#
- Category
- C#
- Title
- unity indestructible
- Category
- C#
- Title
- No IUserTwoFactorTokenProvider<TUser> named 'Default' is registered.'
- Category
- C#
- Title
- how to add a list to observablecollection in c#
- Category
- C#
- Title
- asp net identity include phone number when registering
- Category
- C#
- Title
- unity coroutine
- Category
- C#
- Title
- c# docstring
- Category
- C#
- Title
- razor preview
- Category
- C#
- Title
- divide string in chunks c#
- Category
- C#
- Title
- c# find duplicates in list of strings
- Category
- C#
- Title
- create a file in the directory of the exe and write to it c#
- Category
- C#
- Title
- c sharp index of substring
- Category
- C#
- Title
- c# join array
- Category
- C#
- Title
- how to change text to bold through script unity
- Category
- C#
- Title
- c# postfix increment operator overload
- Category
- C#
- Title
- how to find the type of a object c#
- Category
- C#
- Title
- take screenshot in c#
- Category
- C#
- Title
- unity find object by name
- Category
- C#
- Title
- c sharp substring
- Category
- C#
- Title
- c# stop loop
- Category
- C#
- Title
- c# how to run external program
- Category
- C#
- Title
- Could not load file or assembly 'Ubiety.Dns.Core, Version=2.2.1.0
- Category
- C#
- Title
- windows form rounded corners
- Category
- C#
- Title
- object escape player unity
- Category
- C#
- Title
- abstract class c#
- Category
- C#