unity getcomponent
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;
}
}
} //First script
public class Enemy : MonoBehaviour{
public int health = 10;
public void saySomething(){
Debug.Log("Hi, i am an enemy.");
}
}
//Second script
//There is one thing though,
//if the script is located on another gameobject,
//which will be most likely, you have to somehow
//find that gameobject first. There are many ways of
//doing it, via raycast, collision,
//trigger, find by tag, find by gameobject etc.
//But i will use the simplest way to understand,
//that is declaring public GameObject in player script,
//which contains Enemy, and later you just drag and drop the
//gameobject in the inspector.
public class Player : MonoBehaviour{
//Drag and drop gameobject with script Enemy in inspector
GameObject enemyGameObject;
void Start(){
//We get Enemy component - script
//from public gameobject which contains this script
Enemy enemyScript = enemyGameObject.GetComponent<Enemy>();
//This will debug Enemy health is: 10
Debug.Log("Enemy health is:"+ enemyScript.health)
//This will debug "Hi, i am an enemy."
enemyScript.saySomething();
}
}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;
}
}
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 tagusing 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
- c# string to memorystream
- Category
- C#
- Title
- c# for loop without increment
- Category
- C#
- Title
- copy a list C#
- Category
- C#
- Title
- c# rsa example
- Category
- C#
- Title
- how to exit a program in c#
- Category
- C#
- Title
- c# get desktop path
- Category
- C#
- Title
- c# array isn't working
- Category
- C#
- Title
- how to add array to list in c#
- Category
- C#
- Title
- 5 second timer in c#
- Category
- C#
- Title
- add getenumerator to class c#
- Category
- C#
- Title
- Exception thrown: 'System.FormatException' in mscorlib.dll dates
- Category
- C#
- Title
- .net core session
- Category
- C#
- Title
- c# cheat sheet
- Category
- C#
- Title
- c# making a folder
- Category
- C#
- Title
- : ? conditioanl statement c#
- Category
- C#
- Title
- how to generate random unique id in c#
- Category
- C#
- Title
- how to convert int to string unity c#
- Category
- C#
- Title
- 2D follow ia unity 2D with agrorange
- Category
- C#
- Title
- invalidoperationexception c# ui thread
- Category
- C#
- Title
- c# remove specific character from string
- Category
- C#
- Title
- csharp datetime string format
- Category
- C#
- Title
- repeater itemdatabound event in c#
- Category
- C#
- Title
- unity accessing 2d pointlight from c# script
- Category
- C#
- Title
- change partial view based on select asp.net core
- Category
- C#
- Title
- unity list
- Category
- C#
- Title
- how to make a pause feautre in unity
- Category
- C#
- Title
- c# generic abstract method
- Category
- C#
- Title
- c# combobox datasource enum
- Category
- C#
- Title
- find month number from date C#
- Category
- C#
- Title
- move towards target unity
- Category
- C#
- Title
- c# how to exit program
- Category
- C#
- Title
- c# reverse a string
- Category
- C#
- Title
- docker ssh
- Category
- C#
- Title
- order by C#
- Category
- C#
- Title
- convert system.byte a string c#
- Category
- C#
- Title
- c# generate random number
- Category
- C#
- Title
- c# yield
- Category
- C#
- Title
- how to make an object move in unity
- Category
- C#
- Title
- create object in c#
- Category
- C#
- Title
- using in c#
- Category
- C#
- Title
- c# create array of int
- Category
- C#
- Title
- unity gizmo draw line
- Category
- C#
- Title
- how to add a gameobject
- Category
- C#
- Title
- No context type was found in the assembly
- Category
- C#
- Title
- decimal to string whole number c#
- Category
- C#
- Title
- c# EncoderParameter
- Category
- C#
- Title
- unity button interactable
- Category
- C#
- Title
- void start
- Category
- C#
- Title
- newtonsoft create dynamic object
- Category
- C#
- Title
- c# set textbox text
- Category
- C#
- Title
- wpf use enum description
- Category
- C#
- Title
- dynamic group by expression C#
- Category
- C#
- Title
- degree to radians c#
- Category
- C#
- Title
- c# read file stream
- Category
- C#
- Title
- C# Into To Tring Debug.Log
- Category
- C#
- Title
- how to reduce garbage collection c#
- Category
- C#
- Title
- dynamically add rows to datagridview c#
- Category
- C#
- Title
- call Textboxfor in cs
- Category
- C#
- Title
- string to enum c#
- Category
- C#
- Title
- add row and columns to grid wpf in code
- Category
- C#
- Title
- get user directory of file in c#
- Category
- C#
- Title
- c# get enum value from string
- Category
- C#
- Title
- unity gameobjects with tag
- Category
- C#
- Title
- how to get value from object in c#
- Category
- C#
- Title
- c# remove from list in foreach
- Category
- C#
- Title
- c# relaxed boolean cast
- Category
- C#
- Title
- how to disable a gameObject unity c#
- Category
- C#
- Title
- c# add object to array
- Category
- C#
- Title
- resize image c#
- Category
- C#
- Title
- c# getasync response
- Category
- C#
- Title
- read configuration workerservice
- Category
- C#
- Title
- zoom gedit
- Category
- C#
- Title
- epplus excel vb.net
- Category
- C#
- Title
- c# array to list
- Category
- C#
- Title
- c# webrequest cookies
- Category
- C#
- Title
- c# return two variables of different types
- Category
- C#
- Title
- linq foreach c#
- Category
- C#
- Title
- set width of rect transform unity
- Category
- C#
- Title
- C# datareadeer return null
- Category
- C#
- Title
- c# linq to select even numbers
- Category
- C#
- Title
- .net core download image from url binary file
- Category
- C#
- Title
- c# size of enum
- Category
- C#
- Title
- unity rotate towards mouse
- Category
- C#
- Title
- decalre an int list mvc
- Category
- C#
- Title
- blazor wasm routable page in separate project
- Category
- C#
- Title
- c# get full URL of page
- Category
- C#
- Title
- c# windows grab screenshot
- Category
- C#
- Title
- c# regex to find number between parenthesis
- Category
- C#
- Title
- c# sort array
- Category
- C#
- Title
- go right unity
- Category
- C#
- Title
- how to get specific length of row in matrix c#
- Category
- C#
- Title
- c# implement ienumerable t
- Category
- C#
- Title
- http post request login example asp.net c#
- Category
- C#
- Title
- unity deactivate scripts in list
- Category
- C#
- Title
- socket in c#
- Category
- C#
- Title
- 2d object look at object
- Category
- C#
- Title
- how to spawn coins randomly around the screen unity 2d
- Category
- C#
- Title
- json serialize object capitalization config
- Category
- C#
- Title
- visual studio clear text script
- Category
- C#
- Title
- unity set sprite transparency
- Category
- C#