C# GetComponent in Unity! - Beginner Scripting Tutorial

preview_player
Показать описание
Watch this video in context on Unity Learn:

How to use the GetComponent function to address properties of other scripts or components.
Рекомендации по теме
Комментарии
Автор

Really what you need to know, GetComponent by itself is a "LOCAL" function (or rather local to a single game object) that grabs components on game objects that the script is attached to. If you want to grab components on OTHER game objects, you have to REFERENCE those game objects into the script first, THEN you can use the local GetComponent once the other object is referenced in.


*public GameObject otherGameObject;* this is the reference to ANOTHER object in the game. NOW we can use GetComponent on that object like usual. Without this, you can only access components on the game object that the script is attached to.


*private variable = component here>();* this is creating a new variable in the current script and giving us access to the other game object's components.

Andrelas
Автор

10 seconds of this video helped me more than 1 hour trying to figure this out by myself. Thanks!

GamingEdwards
Автор

The naming conventions were unnecessarily obtuse and I walked away with more questions than answers. I'd recommend making the video longer with several examples, starting each from scratch.

captainbusketttv
Автор

If anyone watching this is still confused like I was, look up Charger Games - GetComponent Function in Unity C#

stereohalf
Автор

lol. They left out the hard and useful part. You see, it's easy to use GetComponent when you can first use a 'public otherGameObject' and then drag and drop some GameObject into there. Then you just reference that with GetComponent. Easy! And then.... you start using preFabs and you can't do this. But you still need that original otherGameObject reference. So you have to code it in using GameObject.find - hope that saves you all the trouble I went through. Remember... step one is referencing the other GameObject. And if you can't drag and drop you have to use GameObject.find.... and there are several options. Find by name. Find by tag, etc. Also... when you do this, you learn that gameObjects are created in a sequence so if you try to refer to one that hasn't been created yet it will return null. Good luck everyone.

godsofthesingularity
Автор

Thank you. This tutorial really helped me I couldn't find a solution for a few hours and this is the first thing that worked.

bogdanpoznanic
Автор

Thanks. I’ll try this out. I’m familiar with other programming languages, but I found unity’s scripting for accessing other objects and the object’s components difficult.

I think this new info will help me out.

thunderstruck
Автор

Hi everyone, just a question to fully understand this (very good) tutorial (imo): The anotherScript and yetAnotherScript variables here hold instances of the two custom classes AnotherScript and YetAnotherScript which can then be accessed in this class (which is UsingOtherComponents) ; is that correct?

SebastianPortilloMusic
Автор

So if GetComponent is expensive, but I still need constant access to another script's variables and methods, what option do I have?

zeramino
Автор

Danndx's "Unity C# - How to interact between scripts" will help you much better

ramiru
Автор

So lets say I'm trying to get a separate script component from the parent object of my script, as you are in your first example.
What happens if there are multiple components of the target type belonging to the parent? Is only the first one returned? Is a list returned? What is the behavior?

Tiparium_NMF
Автор

estava horas pesquisando sobre um erro que estava dando neste GetComponent

NDRSNSSS
Автор

thank you.
I have a unity issue now.
can you help me?

vrargamedevelopment
Автор

ive been trying to do somthing and i just cant figure it out
im willing to try pretty much anything at this point

using UnityEngine;

public class Orange_Block : MonoBehaviour
{
public bool moves;

public move Move;

void Describe()
{
Move = GetComponent<move>();
}

void OnTriggerEnter()
{
Move = GetComponent<move>();

if (Input.GetKey("a") || Input.GetKey("d"))
{
moves = true;
}
else
{
moves = false;
}

if (moves == false)
{
Move.enabled = false;

}
}
}

"move" is the component im trying to acces from another object and "Move" is what i named it
any clues in what im doing wrong?

ImJestFaiq
Автор

Hello, I had a question. In C#, we were doing the instance import by saying new. Here, we do it by saying GetCompanent. Are these two processes actually the same thing? Confusingly, when the new update was made, the entered values were stuck somewhere in the stack. In GetCompanent, we can see the values of an object we received from the inspector. So in a way, we're reaching. The two seem a little different to me. I would be glad if you enlighten me.

sercansaglam
Автор

as said in the video, using 'getcomponent' is expensive in terms of processing power. So in place of that can we use a public variable of type 'anotherscript' and then attach that script in the unity editor??

AdityaRaj-bqdz
Автор

THANK U, BETTER ORIGINAL THAN THE OTHER ONES

xavieracaling
Автор

I can't seem to reference my script, I have been following it through but I am still having an issue. The scripts are on the same gameobject so I am not sure what is the issue

DavidBrit
Автор

This is very good, thank you for the help.

X-VIPRIN
Автор

I have a question, what if I what to get some values of an object, like the diameter of a spere, what should I do?

dragonenelaire