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 /
avatar image
0
Question by BillyMFT · Nov 17, 2016 at 12:34 AM · referenceclasses

access gameobject from instance of class

Let's say I have GameObjectA that has ComponentA on it. Inside ComponentA I create an instance of ClassB (that preferably does not extend MonoBehaviour). Is there a way that ClassB can reference GameObjectA without passing GameObjectA to the ClassB constructor?

Thanks

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
1

Answer by jmgek · Nov 17, 2016 at 01:55 AM

This is extremely confusing, but what I think you're trying to do is reference gameobject A inside the instanced Class B that was created in Class A...

Is there a reason you cant pass it?:

 ClassB classb = new ClassB(this.gameObject);

That's the best way, but if you need to (and I find this silly):

 class GameManager
 {
     static GameObject gameObjectA;
     
     void Start()
     {
         ClassB classB = new ClassB();
         gameObjectA = this.gameObject; 
     }
 }

 public ClassB
 { 
     GameObject instancedGameObjectA; 
 
     void ClassB()
     {
          instancedGameObjectA = GameManager.gameObjectA; 
     }
 }

There may be a better way, but this is from the top of my head. Good luck!

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 BillyMFT · Nov 17, 2016 at 02:25 AM 0
Share

Thanks for the reply. I know I could pass the gameObject in the constructor and I mentioned that in my question, but I guess I was wondering if there was a cleaner way, so in ClassB saying something like

GameObject gameObjectA = this.instantiatingClass.gameObject;

$$anonymous$$ind of like how you can refer to the base class in a subclass.

Doesn't sound like there is though.

Thanks again

avatar image jmgek BillyMFT · Nov 17, 2016 at 02:33 AM 0
Share

In my second piece of code I'm not passing anything to the constructor, I'm referencing the static gameObject that instanced the current ClassB in the constructor. It's basically what you're asking to do.

To my knowledge you can't instance a class and reference what instanced it in c#. I may be wrong,

avatar image BillyMFT jmgek · Nov 17, 2016 at 02:40 AM 0
Share

O$$anonymous$$ thanks again

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to pass over different scripts, subscribing to the same interface 0 Answers

Assigning class reference dynamically? 1 Answer

Instantiating a class using dragged-in references 0 Answers

How do I reference variables from other class files? 1 Answer

Im not sure why this works, Class referencing & memory question, 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