Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by esitoinatteso · Oct 07, 2013 at 02:13 PM · gameobjectgetcomponenthowtoanother

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
13
Best Answer

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(); 
    }  
 }











Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image esitoinatteso · Oct 07, 2013 at 03:31 PM 0
Share

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!

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image esitoinatteso · Oct 07, 2013 at 03:27 PM 0
Share

Sorry Shadow$$anonymous$$ but I couldn't get the logic behind it, thanks anyway for giving it a try!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Ok so im trying to get a string's name from another script to enable (SetActive) a certain gameObject in the scene (the player); however I keep getting errors and i have no idea what to do?? any help??? 1 Answer

Is GetComponent<>() slow? 1 Answer

error CS0118: `New_Career.Fame' is a `field' but a `type' was expected 1 Answer

GetComponent of ALL clones? 2 Answers

Getting a Script without setting a variable to the script name 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges