C# GetComponent in Unity! - Beginner Scripting Tutorial

C# GetComponent in Unity! - Beginner Scripting Tutorial

Unity

4 года назад

129,702 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Tiparium
Tiparium - 25.04.2023 21:06

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?

Ответить
STC2
STC2 - 09.04.2023 20:36

This is very good, thank you for the help.

Ответить
Vỹ Nguyễn
Vỹ Nguyễn - 03.02.2023 05:55

<3

Ответить
4ND3RS0N 4SS1S
4ND3RS0N 4SS1S - 18.12.2022 07:25

ajudou!!!!! estava horas pesquisando sobre um erro que estava dando neste GetComponent

Ответить
Sercan Sağlam
Sercan Sağlam - 14.05.2022 18:12

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.

Ответить
MEGA TREE
MEGA TREE - 22.04.2022 08:46

Good scripting

Ответить
Paladin
Paladin - 18.12.2021 20:19

I hate this tutorial so much.

Ответить
Sebastian Portillo
Sebastian Portillo - 13.11.2021 13:24

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?

Ответить
Captain Busket TTV
Captain Busket TTV - 23.06.2021 11:46

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.

Ответить
Im Jest Faiq
Im Jest Faiq - 23.03.2021 15:17

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;
FindObjectOfType<GameOver>().endgame();
}
}
}

"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?

Ответить
Peter Parkour
Peter Parkour - 26.12.2020 23:57

Can we use Getcomponent to get Bools?

Ответить
xavier acaling
xavier acaling - 26.12.2020 01:19

THANK U, BETTER ORIGINAL THAN THE OTHER ONES

Ответить
Mr McCgoo
Mr McCgoo - 22.11.2020 13:08

THANK YOU SO MUCH!!!

Ответить
Aymk gamig vitual Limit
Aymk gamig vitual Limit - 14.11.2020 19:20

Tanks this is really help full

Ответить
Cian Finlay
Cian Finlay - 01.10.2020 12:25

where is the video that was 'linked below on classes'? :) any hookups?

Ответить
Evin Lacy
Evin Lacy - 12.09.2020 07:10

The player's score is over 9000, that's cheeky lol

Ответить
Gods of the Singularity
Gods of the Singularity - 23.07.2020 00:35

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.

Ответить
zeramino
zeramino - 06.07.2020 17:22

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

Ответить
Gaming Edwards
Gaming Edwards - 22.05.2020 17:31

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

Ответить
zorua O.
zorua O. - 21.05.2020 21:32

confusing

Ответить
FTL Studios
FTL Studios - 15.05.2020 00:24

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 = otherGameObject.GetComponent<desired component here>(); <----- this is creating a new variable in the current script and giving us access to the other game object's components.

Ответить
OnHoliday
OnHoliday - 05.05.2020 21:22

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

Ответить
erich giusseppe soto parada
erich giusseppe soto parada - 03.05.2020 19:43

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?

Ответить
Ramiru
Ramiru - 17.04.2020 22:51

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

Ответить
Aditya Raj
Aditya Raj - 12.04.2020 10:29

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??

Ответить
Livelandr
Livelandr - 21.03.2020 15:31

А как редактировать то блять

Ответить
DavidBrit
DavidBrit - 11.03.2020 00:16

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

Ответить
Dragongem Gaming
Dragongem Gaming - 17.02.2020 17:05

NOTHING WORKS!!!!!!!!!!!!!!!!!!!!!!! I have the scripts in the same object and one is named slide and one is named first person controller. From first person controller i and doing private slide slide; and it says thats incorrect

Ответить
VR&AR Game development
VR&AR Game development - 15.12.2019 16:56

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

Ответить
thunderstruck272
thunderstruck272 - 02.12.2019 22:18

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.

Ответить
Oli XRehab
Oli XRehab - 06.11.2019 19:28

this is explained awfully

Ответить
Bingo Bob
Bingo Bob - 04.11.2019 00:59

Hello, I get the error "The type or namespace name 'AnotherScript' could not be found (are you missing a using directive or an assembly reference?)"


I have of course replaced AnotherScript with my actual script name.

Ответить
Bogdan Poznanic
Bogdan Poznanic - 16.08.2019 11:30

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.

Ответить
Carousel Blind.
Carousel Blind. - 27.07.2019 05:32

i think the current design of unity is cool its good for rapid prototyping. ecs is currently confusing alot more code. i know dots/ecs isn't a replacement for the current monobehavior unity will forever have it. dots/ecs is like angular while you only needed basic html which is monobehavior.

Ответить
Inflex Games
Inflex Games - 27.07.2019 03:25

thank you, these videos are very helpful

Ответить
Hüseyin Yıldız
Hüseyin Yıldız - 26.07.2019 20:27

Thanks Unity and Microsoft.But Please Upgrade design system and design interface in Unity.

Ответить
Картавый Шершавый
Картавый Шершавый - 09.07.2019 21:20

hard

Ответить