- Home /
Can't Understand GetComponent C#
Hi there. I realize it's my fault here, because I'm just too dummy to understand how it works from the tons of examples everybody can easily find here or in other sites.
How do I use GetComponent? I've been in the docs, I've visited a lot of questions, I've seen a lot of examples but still I'm getting errors when it comes to write one on my own.
Here is what I've understood of how GetComponent works: You got 2 scripts, script A and script B, there's no need of a parent/child relation. You declare a new public variable in script A, go to script B and then you write...?
For example: I want to make a script to handle all common variables of my enemies. That way I won't have to write those variables for each one of them, it makes sense? Variables like enemySpeed, enemyDamage, and so on. Done that, I create a new c#script and call it with the specific name of an enemy, like Thug, I make it a child of my Enemy script by typing Public Class Thug : Enemy{} and I now want to access and modify all the values I've already written in Enemy. How do I do that? Is this reasonable?
Sorry this is a very basic question but I'm unable to get past it... and because of this, could you please be exhaustive in your answers? like, really verbose and full of examples? Keep in mind that all the existing examples weren't enough to make me understand... I may have clenched that this process is easier in JavaScript though.
Thanks a lot for the patience.
Answer by YoungDeveloper · Oct 07, 2013 at 02:33 PM
You use GetComponent to access something you need in other script.
This is how it works, simple example.
//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();
}
}
Hi YoungDeveloper! Thanks a lot for your answer, it's neat and friendly, and helped me understand the topic! I think I made it! I'm so hyper right now! Thanks again!
Answer by $$anonymous$$ · Oct 07, 2013 at 02:32 PM
Try this, in one of your scripts:
Public static bool test;
In your other script under e.g. void FixedUpdate(){
Getcomponent <NameofScript> ();
If (getMouseButtonDown(0) {
NameofScript.test = true; // Look what I did, called that variable from another script :)
}
If you get the logic behind that, it all falls into place.. It's good for calling variables between scenes etc.
Sorry Shadow$$anonymous$$ but I couldn't get the logic behind it, thanks anyway for giving it a try!
Your answer
Follow this Question
Related Questions
GetComponent vs AddComponent 3 Answers
C# GetComponent Issue 2 Answers
Acessing a Script Instance from Another Object 3 Answers
how to lock screen? 1 Answer
What is the difference between GameObject.GetComponent and Component.GetComponent? 1 Answer