how to get component in unity c#
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 tagpublic 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
}rb = GetComponent<Rigidbody>(); //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();
}
}
Also in C#:
- Title
- c# MD5.Create returning nul
- Category
- C#
- Title
- send type as argument c#
- Category
- C#
- Title
- class selector to property in asp net core dropdown
- Category
- C#
- Title
- c# zip a file
- Category
- C#
- Title
- what is a protected int c#
- Category
- C#
- Title
- change partial view based on select asp.net core
- Category
- C#
- Title
- what is type unity
- Category
- C#
- Title
- c# request run as administrator
- Category
- C#
- Title
- uwp file open picker
- Category
- C#
- Title
- how to get value from object in c#
- Category
- C#
- Title
- add row count devepxress report
- Category
- C#
- Title
- instantiate object in circle
- Category
- C#
- Title
- binding c#
- Category
- C#
- Title
- c# EncoderParameter
- Category
- C#
- Title
- leantween id
- Category
- C#
- Title
- how to raycast unit
- Category
- C#
- Title
- c# pull request
- Category
- C#
- Title
- c# md5 hash file
- Category
- C#
- Title
- how to convert iformfile to byte array c#
- Category
- C#
- Title
- read configuration workerservice
- Category
- C#
- Title
- create line in unity
- Category
- C#
- Title
- How to get the world position of the edge of an object?
- Category
- C#
- Title
- C# Into To Tring Debug.Log
- Category
- C#
- Title
- string to enum c#
- Category
- C#
- Title
- unity docs player input
- Category
- C#
- Title
- compose graphql query string in c#
- Category
- C#
- Title
- mvc c# w3schools
- Category
- C#
- Title
- how to add a componet to a gameobject throgh code unity
- Category
- C#
- Title
- c# create a text file
- Category
- C#
- Title
- sum the digits in c#
- Category
- C#
- Title
- c# random string
- Category
- C#
- Title
- replace all ponctuation characters c#
- Category
- C#
- Title
- c# read file stream
- Category
- C#
- Title
- httpcontext in .net standard
- Category
- C#
- Title
- c# format string to date yyyymmdd
- Category
- C#
- Title
- ping with c#
- Category
- C#
- Title
- unity make a int arry with preset values
- Category
- C#
- Title
- animations for pause menu
- Category
- C#
- Title
- c# inheritance constructor
- Category
- C#
- Title
- c# return two variables of different types
- Category
- C#
- Title
- c# how to take from a float
- Category
- C#
- Title
- c# httpclient azure function authorization
- Category
- C#
- Title
- variables
- Category
- C#
- Title
- csharp datetime string format
- Category
- C#
- Title
- c# stringbuilder
- Category
- C#
- Title
- unity get max occurrence in list
- Category
- C#
- Title
- unity cycle children
- Category
- C#
- Title
- c# windows forms draw pixel
- Category
- C#
- Title
- C# save pdf stream to file
- Category
- C#
- Title
- unity how to rotate something to point to something else
- Category
- C#
- Title
- c# declare an int list
- Category
- C#
- Title
- exception handling c#
- Category
- C#
- Title
- displayname c#
- Category
- C#
- Title
- value is null to insert in c#
- Category
- C#
- Title
- how to work with ascii in c#
- Category
- C#
- Title
- loop gridcontrol devexpress c#
- Category
- C#
- Title
- and unity
- Category
- C#
- Title
- c# retrieve files in folder
- Category
- C#
- Title
- asp.net c# set session timeout
- Category
- C#
- Title
- unix time c#
- Category
- C#
- Title
- drag object unity 2d
- Category
- C#
- Title
- razor: show editable list
- Category
- C#
- Title
- unity gizmo draw line
- Category
- C#
- Title
- c# how to run external program with args
- Category
- C#
- Title
- date time heutiges datum
- Category
- C#
- Title
- check distance to gameobject
- Category
- C#
- Title
- c# find duplicates in list
- Category
- C#
- Title
- basic sorting algorithms c#
- Category
- C#
- Title
- c# dictionary literal initializer
- Category
- C#
- Title
- unity prevent system from creation
- Category
- C#
- Title
- how to make an object move in unity
- Category
- C#
- Title
- convert array object to int[] c#
- Category
- C#
- Title
- set width of rect transform unity
- Category
- C#
- Title
- c# move with arrow keys
- Category
- C#
- Title
- c# inline initialize dictionary
- Category
- C#
- Title
- c# inotifypropertychanged best practices
- Category
- C#
- Title
- c# reverse string
- Category
- C#
- Title
- get last element of array c#
- Category
- C#
- Title
- add object to list c#
- Category
- C#
- Title
- c# check if string is only letters and numbers
- Category
- C#
- Title
- c# skip following code in loop
- Category
- C#
- Title
- list c#
- Category
- C#
- Title
- C# array to string
- Category
- C#
- Title
- mvc input type file
- Category
- C#
- Title
- what type of variable is true or false in c#
- Category
- C#
- Title
- unity get data from firebase
- Category
- C#
- Title
- get string character by index c#
- Category
- C#
- Title
- radians to degree c#
- Category
- C#
- Title
- how to convert a number to 2 decimal places in c#
- Category
- C#
- Title
- list.addrange in c#
- Category
- C#
- Title
- c# get the last item in a list
- Category
- C#
- Title
- c# md5 string
- Category
- C#
- Title
- c# discord bot
- Category
- C#
- Title
- abstract class c#
- Category
- C#
- Title
- c# print expression tree
- Category
- C#
- Title
- vb.net drag window without titlebar
- Category
- C#
- Title
- F# convert generic.List to list
- Category
- C#
- Title
- c# map
- Category
- C#
- Title
- where keyword in c#
- Category
- C#
- Title
- convert double to currency c#
- Category
- C#