Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by Skeletim · Aug 03, 2013 at 09:31 PM · gameobjectmonobehaviourclassescustom

Accessing Monobehaviour classes on other objects

I have created a small class with a variable that I wanted to attach to an empty game object. I had it inherit from Monobehaviour because I wanted to be able change the variable value from the inspector.

Since I can't use constructors on classes that are derived from that, how do I gain access to that variable on that object (or any object with one) from another script?

I'm sure this is probably an easy answer, and I just don't know how to ask the question in Google to get the correct response

Comment
Add comment · Show 2
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 Lovrenc · Aug 03, 2013 at 10:08 PM 0
Share

Every day. Every damn day this question is asked at least once.

avatar image Eric5h5 · Aug 03, 2013 at 10:57 PM 0
Share

To be fair, at least the question asker is aware of the nature of this question. Putting "unity access variable on another script" or similar into Google will get plenty of results. So basically just ask your question on Google and be sure to put "unity" in there.

1 Reply

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

Answer by Eric5h5 · Aug 03, 2013 at 09:33 PM

GetComponent.

Comment
Add comment · Show 3 · 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 Skeletim · Aug 05, 2013 at 02:16 AM 0
Share

Thanks for the kind reply Eric. Next time, I'll be sure to do more research and solve the problem myself, so I don't offend others. I know that I may be asking some questions that probably didn't need to be asked, but I will try to update my comments so that people will see this in future searches.

They have a short tutorial on this at this web link. http://unity3d.com/learn/tutorials/modules/beginner/scripting/getcomponent

For me at least, I found this to not work. They attach the script to a gameobject, but they don't find the object. I don't know why it seems to run in the video opposed to my test scene.

I had to type the former opposed to the latter.

 yetAnotherScript = GameObject.Find("Cube").gameObject.GetComponent<YetAnotherScript>();

 yetAnotherScript = otherGameObject.GetComponent<YetAnotherScript>();

The latter gave me Null References for some reason. Just wanted to post this in case other people come searching for this.

avatar image Eric5h5 · Aug 05, 2013 at 02:42 AM 0
Share

If you get a null reference error with that, it's because otherGameObject wasn't defined.

avatar image Skeletim · Aug 05, 2013 at 03:31 PM 0
Share

In the script on the tutorial site, they have

  public GameObject otherGameObject;
  private YetAnotherScript yetAnotherScript;

In their tutorial, I don't see otherGameObject being assigned a specific object, so I figured that much, that it was just a null object. Although, in the video it runs when they click play.

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

16 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

Related Questions

Classes : extend MonoBehaviour issues 1 Answer

How to structure code for squad selection and movement 0 Answers

Is it possible to find gameobjects as .cs? 1 Answer

Question about classes on C# 1 Answer

GameObjects with custom properties into a GUI textbox. 0 Answers


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