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
0
Question by DeaD4MaN · Sep 04, 2014 at 12:54 PM · gameobjectcomponentcustomlogicextension

Adding a global custom component to GameObjects

I'm current trying to understand some of the logic behind unity engine and just can't wrap my head around this. Unfortunately looking at declarations and assemblies doesn't help at all.

Every GameObject in unity can have a transform, collider, rigidbody, renderer, etc as these properties are defined in class "Component", that derives from "Object".

My question is, how can I create a custom component (ie "MyGlobalComponent" with public field "delta") that every GameObject will have and can be accessed via code, for example:

 public class MyClass : MonoBehaviour
 {
     public GameObject go; //set in editor
     public int myInt = 0;
 
     void Update ()
     {
         myInt = go.MyGlobalComponent.delta;
     }
 }

Thank you.

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

1 Reply

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

Answer by OrbitGames · Sep 04, 2014 at 01:13 PM

You need to create a reference to the "MyGlobalComponent".

This can be done via getComponent.

You can also do it without if your variable was static, that means the same for every gameobject.

In your case it would be (untested):

 myint = transform.GetComponent<MyGlobalComponent>().delta;

if you want it from another you GO you should use

 myint = go.transform.GetComponent<MyGlobalComponent>().delta;


hope this helps

Comment
Add comment · Show 2 · 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 DeaD4MaN · Sep 04, 2014 at 01:46 PM 0
Share

Does "$$anonymous$$yGlobalComponent" must inherit from "Component" or any other class? And must I use AddComponent in the script that is attached to the "go" GameObject?

avatar image DeaD4MaN · Sep 05, 2014 at 08:38 AM 0
Share

As expected, I need to add "$$anonymous$$yGlobalComponent" to each GameObject manually via editor or code. What I'm trying to achieve a is a custom component that each GameObject has automatically (like transform).

Although thanks, reading into get/set Component, I understand it usefulness much better now.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Can I move a component from one GameObject to another in script? 1 Answer

Simple text in inspector components? 2 Answers

How to mimic Unity's Components based System 1 Answer

How to Check if a component Exists on a Gameobject. 3 Answers

Storing component of instance in an array 2 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