• 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
0
Question by Ronin Davis · Apr 26, 2015 at 01:40 AM · getcomponent

GetComponent not working

Hi, i was working on a shoot script and an AI script and i needed to access a variable from the AI script, this is what i did:

 var soldierAI : SoldierAI;
     
 function Start () {
     soldierAI = GetComponent("SoldierAI");
 }

This is what it says to do on the script reference page and many other forum pages, but it doesn't seem to be working. Can someone please help me by telling me the proper way to do this.

Comment
Add comment · Show 5
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 tanoshimi · Apr 26, 2015 at 07:26 AM 1
Share

"Not working"...? Have you turned your computer on? Attached the script to an object in the scene? Hit Play?

If you want more specific answers, include a more specific description.

avatar image Ronin Davis · Apr 26, 2015 at 07:50 AM 0
Share

I'v tried so many syntax that people have posted on other forums and other questions but none of them work i've tried the one on the unity scripting api documentation, i've tried the ones in the tutorial on getcomponent, i've tried loads of things but none of them work. The one on the documentation doesn't say anything about scripts. The one on the tutorial is the one in the question i asked, and it doesn't use a proper type of variable, i have also tried it in Csharp and it doesn't work. other ones don't work either. I just want to know the correct syntax.

avatar image Calum1015 · Apr 27, 2015 at 03:32 AM 0
Share

Okay, I'm going to try and make this as simple as possible. First step. Re Install unity to check if this is a bug on your version. Second step. Once unity has successfully installed or while it is installing thoroughly read the manual on the GameObject's section. I'll even help you out, here's a link. http://docs.unity3d.com/$$anonymous$$anual/GameObjects.html ||Read everything in that section. Rewrite your code from the ground up using the manual. And yourself. Ins$$anonymous$$d of co$$anonymous$$g immediately to Unity Answers debug it yourself. Still doesn't work? Re-read the manual. Then come here again. Good Luck.

Oh and before I forget: http://docs.unity3d.com/ScriptReference/GameObject.html that should help even $$anonymous$$OR$$anonymous$$

avatar image Ronin Davis · Apr 27, 2015 at 03:51 AM 0
Share

I have read through that page you sent me and it didn't help what so ever. It is not a bug it is a scripting error. I did and always do read the documentation before asking. I have rebuilt that part of the script a thousand times with different ways of doing it. Thank you.

avatar image Fappp · May 08, 2015 at 06:58 PM 0
Share

Sometimes it helps to do this, don't know why but hope it helps for you too:

 var soldierAI : SoldierAI;
      
  function Start () {
      soldierAI = this.GetComponent(SoldierAI);   // No "" here and call this. to be sure
  }

also make sure the variables are public. If they come from another class make sure you have the constructors right.

I just read below comments. Can you please show me the SoldierAI script and the variable you need to access?

5 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by mkprojeckt · Jan 14, 2016 at 11:46 AM

If the GameObject or component is by default deactivated and has not awaken, you won't be able to find it. You can use the overloaded for GetComponentsInChildren, which accepts a bool as parameter. Setting it to true will also search for disabled gameObjects. I don't know why this is the default behaviour, but it is. Hope It heps you.

Comment
Add comment · 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
0

Answer by Calum1015 · Apr 26, 2015 at 03:54 AM

http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

Comment
Add comment · Show 5 · 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 Ronin Davis · Apr 26, 2015 at 03:56 AM 0
Share

I've tried it, it doesn't work with scripts

avatar image Bonfire-Boy · Apr 26, 2015 at 04:52 AM 0
Share

It does work with scripts. What exactly is happening when you do it? Are you sure there is actually a SolderAI component on the GameObject?

avatar image Ronin Davis · Apr 26, 2015 at 04:58 AM 0
Share

I have tried this before:

 var soldier : GameObject;
 var soldierAI : SoldierAI;
 
 function Start (){
 
 soldierAI = soldier.GetComponent(SoldierAI);
 
 }

It returns with an error saying: The name 'SoldierAI' does not denote a valid type ('not found'). Did you mean 'UnityEngine.UI.Slider'? $$anonymous$$eaning that there is no type soldierAI, so i don't know what to use.

p.s. there is a script called "SoldierAI" attached to the object

avatar image Cherno · Apr 27, 2015 at 12:20 AM 0
Share

This error message makes me supsicious. Are you 1000% sure that there exists a class with that exact name? $$anonymous$$aybe you named the class "soldierAI" ins$$anonymous$$d of "SoldierAI", or even used a lowercase "L" ins$$anonymous$$d of an uppercase "I" ?

avatar image Ronin Davis · Apr 27, 2015 at 02:03 AM 0
Share

I named it exactly that it just doesn't want to work for some reason.

avatar image
0

Answer by n5ep · Apr 26, 2015 at 11:42 AM

You need to access the component of a game object. As the example in the API:

 // Disable the spring on the HingeJoint component.
 
 var hinge : HingeJoint;
 hinge = gameObject.GetComponent(HingeJoint);
 hinge.useSpring = false;

You need to refer to the gameobject that has "SoldierAI" script. If this is the same game object (just a different script), you can simply do:

 soldierAI = transform.GetComponent("SoldierAI");

Comment
Add comment · Show 4 · 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 Ronin Davis · Apr 26, 2015 at 11:59 AM 0
Share

I have tried that a thousand times and it doesn't work. Obvcourse i checked the API documentation before asking, that's the first thing i do.

avatar image n5ep · Apr 26, 2015 at 12:52 PM 0
Share

Hi Ronin Davis, you are right - it does not need inverted commas around the script name. Please check this example project that I quickly put together for you to see how it works. Script1 and Script2 are both put on the same GameObject and a public variable in Script2 is being accessed from Script1. Note that I have used

 gameObject.GetComponent()

but you can also use

 transform.GetComponent()

Hopefully this makes it clear to you about how to access other scripts. :)

PS: I have just zipped the project folder and attached it here, not 100% sure if you'll be able to open it. If not, just get the two scripts from the Assets folder and attach them to a GameObject (empty, cube, whatever).

externalscript.zip (350.9 kB)
avatar image Bonfire-Boy · Apr 27, 2015 at 10:00 AM 0
Share

"Obvcourse i checked the API documentation before asking, that's the first thing i do."

Get a grip. You haven't even told us what's going wrong yet.

avatar image Ronin Davis · Apr 27, 2015 at 10:33 AM 0
Share

Sorry i wasn't meaning to sound ignorant i was just say that i checked the API first.

avatar image
0

Answer by AcE_fLoOdEr · Apr 26, 2015 at 05:08 AM

When you create a variable of a class for example, you created a variable out of the class SoldierAI. In the inspector, it should give you an empty box where you can select an object that has that class attached to it... All you gotta do is select your desired object.

You can then access the variables if they are public obviously...

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 Ronin Davis · Apr 26, 2015 at 05:42 AM 0
Share

It hasn't done that for me, i might just be reading your answer wrong though.

avatar image
0

Answer by morphman86 · Apr 26, 2015 at 09:25 PM

I might be getting this wrong, but it looks like you want to access the script SoldierAI.

You almost got it right, but but GetComponent need to be written slightly different.

Try this:

 var soldierAI : SoldierAI;
          
      function Start () {
          soldierAI = transform.GetComponent.<SoldierAI>();
      }

This will get the script by that name in the same GameObject. If you have the script in another object, you need to trade "transform" on line 3 to the object in question.

Comment
Add comment · Show 7 · 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 Fappp · May 08, 2015 at 07:01 PM 0
Share

This is C# way of doing it.

avatar image morphman86 · May 08, 2015 at 07:04 PM 1
Share

It works fine in my JavaScript, in fact I did a test with this exact code using only JavaScript and it worked perfectly.

avatar image Fappp · May 08, 2015 at 07:07 PM 0
Share

[http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html][1] Check the JS and C# tab ;) Unity docs say otherwise! Proper testing anyways! ;) [1]: http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

avatar image morphman86 · May 08, 2015 at 07:10 PM 0
Share

I think that the docs might not be updated properly, because if you try to use that in an actual code (at least on Unity5), it will give the same problem that OP has, and the code that I wrote above (the "C# method") works.

avatar image Fappp · May 08, 2015 at 07:12 PM 0
Share

The Type accessed has to be setup properly, I need to see the soldier AI script for this.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Collection Using "GetComponent" 1 Answer

Transfering variable between gameojects and scripts 2 Answers

How to use GetComponent in a foreach loop? 3 Answers

Implicit type GetComponent call 0 Answers

How do i read the state of an enumurator on another object? 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