Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Hyperactive · Nov 17, 2015 at 10:58 AM · gameobjectscript.getcomponent

Store script from GetComponent ?

Hi All,

Trying to figure out how to store a script from GetComponent.

For example:

 private gameobject storeHere;
 
 void Start ()
 {
 storeHere: gameObject.GetComponent<ThisComponent>();
 }

But this doesn't work, because it's not a gameobject obviously... it's a script...But what can I store a script in ?

Cheers !

Comment
Add comment · Show 3
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 MayankOctro · Nov 17, 2015 at 11:08 AM 0
Share

Question is not clear. Do you want to get a script from a game object or add and get the script from the game object cause you have declared gameobject private. and you should use private GameObject storeHere;.

avatar image Hyperactive MayankOctro · Nov 17, 2015 at 11:11 AM 0
Share

I want to just get a script from a game object. The issue with Gameobject is that it just returns: The type or namespace name Gameobject could not be found.

avatar image MayankOctro Hyperactive · Nov 17, 2015 at 11:14 AM 0
Share

GameObject. edited

1 Reply

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

Answer by Baste · Nov 17, 2015 at 12:11 PM

If you want to store a variable of the type ThisComponent, the variable needs the type ThisComponent:

 private ThisComponent storeHere;
 
 void Start() {
     storeHere = GetComponent<ThisComponent>();
 }

Note that gameObject.GetComponent is not neccessary, it does the same thing as just GetComponent.

If by ThisComponent you mean the current MonoBehaviour, you don't need to store it, ever, as it's available through the keyword 'this'.

If you want to store reference to a script, but don't know the exact type of the script, you're looking for the class MonoBehaviour, as that's what all of the scripts that you can attach to GameObjects inherit from. So that'd be:

 private MonoBehaviour storeHere;

Though that's unlikely, as you won't have access to any of the methods or variables defined in the script.

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 meat5000 ♦ · Nov 17, 2015 at 12:37 PM 0
Share

I think in JS you need a dot after GetComponent.

 storeHere = GetComponent.<ThisComponent>();
avatar image Baste meat5000 ♦ · Nov 17, 2015 at 01:09 PM 0
Share

Since they're writing

 void Start()

ins$$anonymous$$d of

 function Start()

I'm assu$$anonymous$$g the question's about C#. But, yeah, UnityScript has the absurd extra dot for some reason.

avatar image meat5000 ♦ Baste · Nov 17, 2015 at 09:58 PM 0
Share

Yup, just good to be complete ;)

avatar image Hyperactive · Nov 17, 2015 at 05:34 PM 0
Share

This worked like a charm ! Thanks a lot for your help and explanations. Very clear and extremely helpful.

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

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

I need quick help with my C# code GetComponent! 1 Answer

GetComponent is not a member of Object 0 Answers

How do I run an animation then destroy a game object? 1 Answer

GameObject changes to None in inspector after pressing Play 0 Answers

How to enable and disable a script on an game object by pressing a keyboard button? 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