getcomponent c#
C#
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
} //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
- bulk update in c# using jquery datatble
- Category
- C#
- Title
- c# stop loop
- Category
- C#
- Title
- c# get index of item in list
- Category
- C#
- Title
- c sharp comments
- Category
- C#
- Title
- set label position winforms
- Category
- C#
- Title
- unity simple fps controller
- Category
- C#
- Title
- c# get country code
- Category
- C#
- Title
- cshtml foreach
- Category
- C#
- Title
- c# get pc ip address
- Category
- C#
- Title
- how to clear console through script unity
- Category
- C#
- Title
- null coalescing operator c#
- Category
- C#
- Title
- unity deactivate scripts in list
- Category
- C#
- Title
- C# .NET Core linq Distinct
- Category
- C#
- Title
- object escape player unity
- Category
- C#
- Title
- 2d character controller unity
- Category
- C#
- Title
- kill child C#
- Category
- C#
- Title
- max of array C#
- Category
- C#
- Title
- iteration c#
- Category
- C#
- Title
- query associative table ef6
- Category
- C#
- Title
- c# param exception
- Category
- C#
- Title
- c# silent execute exe
- Category
- C#
- Title
- how to add to a list c#
- Category
- C#
- Title
- c# date
- Category
- C#
- Title
- c# round to closest multiple
- Category
- C#
- Title
- linq c# object except two lists
- Category
- C#
- Title
- c# how to run external program
- Category
- C#
- Title
- ASP select box all states
- Category
- C#
- Title
- c# reverse string
- Category
- C#
- Title
- windows form rounded corners
- Category
- C#
- Title
- convert string to double c#
- Category
- C#
- Title
- c# length 2d array
- Category
- C#
- Title
- defualtsize UWP c#
- Category
- C#
- Title
- c# error "The name 'ViewBag' does not exist in the current context"
- Category
- C#
- Title
- how to use variables in c#
- Category
- C#
- Title
- check if network is available c#
- Category
- C#
- Title
- unity round vector 3 to nearest integer
- Category
- C#
- Title
- return random from enum
- Category
- C#
- Title
- lista generica como parametro de un metodo en c#
- Category
- C#
- Title
- c# arraylist
- Category
- C#
- Title
- c# docstring
- Category
- C#
- Title
- convert double to currency c#
- Category
- C#
- Title
- unity how to change max fps
- Category
- C#
- Title
- c# keyboard enter
- Category
- C#
- Title
- how to crouch in unity
- Category
- C#
- Title
- windows forms iterate through all controls
- Category
- C#
- Title
- unity c# is not equal to
- Category
- C#
- Title
- wpf restart application c#
- Category
- C#
- Title
- c# get object property value by name
- Category
- C#
- Title
- unity rotate around point
- Category
- C#
- Title
- check connection c#
- Category
- C#
- Title
- unity coroutine
- Category
- C#
- Title
- load information with txt file to uwp c#
- Category
- C#
- Title
- C# get all child classes of a class
- Category
- C#
- Title
- compose graphql query string in c#
- Category
- C#
- Title
- radians to degree c#
- Category
- C#
- Title
- c# listview
- Category
- C#
- Title
- start a particle effect when a button is pushed
- Category
- C#
- Title
- What is a class in c#
- Category
- C#
- Title
- modal dismiss
- Category
- C#
- Title
- trello
- Category
- C#
- Title
- unity line renderer position count
- Category
- C#
- Title
- How to execute script in C#
- Category
- C#
- Title
- where keyword in c#
- Category
- C#
- Title
- c# md5 string
- Category
- C#
- Title
- unity mobile controls
- Category
- C#
- Title
- c# clear list items
- Category
- C#
- Title
- how to stop a form c#
- Category
- C#
- Title
- c# implement ienumerable t
- Category
- C#
- Title
- how to textbox anywhere on chart in c#
- Category
- C#
- Title
- dynamically add rows to datagridview c#
- Category
- C#
- Title
- c# list remove item based on property duplicate
- Category
- C#
- Title
- using in c#
- Category
- C#
- Title
- bool toggle unity c#
- Category
- C#
- Title
- c# print to console
- Category
- C#
- Title
- C# check many strings quickly
- Category
- C#
- Title
- same click event diffrenet buttonms c#
- Category
- C#
- Title
- unity move character
- Category
- C#
- Title
- c# list string return concatenate
- Category
- C#
- Title
- application variable in asp.net core
- Category
- C#
- Title
- calculate impact damage + unity
- Category
- C#
- Title
- asp net identity include phone number when registering
- Category
- C#
- Title
- c list add element
- Category
- C#
- Title
- dynamic convert type c#
- Category
- C#
- Title
- c# replace string case insensitive
- Category
- C#
- Title
- c# try catch error
- Category
- C#
- Title
- change Backcolor c#
- Category
- C#
- Title
- .net core session
- Category
- C#
- Title
- use newtonsoft json to clone object
- Category
- C#
- Title
- wpf make size fill all grid
- Category
- C#
- Title
- c# linq select from object list
- Category
- C#
- Title
- unity movetowards 2d
- Category
- C#
- Title
- c# how to refresh your binding source
- Category
- C#
- Title
- unity rigidbody constraints
- Category
- C#
- Title
- c# display image
- Category
- C#
- Title
- C# array to string
- Category
- C#
- Title
- c# how to check if two lists have same values
- Category
- C#
- Title
- how to add an embedded resource in visual studio code
- Category
- C#
- Title
- asp.net textarea disable resize
- Category
- C#
- Title
- Update data in db .net
- Category
- C#
- Title
- change partial view based on select asp.net core
- Category
- C#