Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 gregsaywhaat · Jun 20, 2019 at 02:27 AM · bug-perhapsinheritanceinterfaces

How to correctly access interface of a subclass?

I'm running into the following situation, and I have absolutely no idea what's going on. By all of my understanding of OOP, this is not how this should work.

 public interface ValueHolder{
     bool ValueHeld();
     void SetHeldValue(bool newVal);
 }

 public class BaseThing : Monobehavior, ValueHolder {
     private bool sneakyValue = true; //Private. Not serialized.

     public bool ValueHeld(){
         return sneakyValue;
     }

     public void SetHeldValue(bool newVal){
         sneakyValue = newVal;
     }
 }

 public class UniqueThing : BaseThing {    } //There are several of these. Performs unrelated tasks.


 //Doing some code things
 GameObject thingObj = Instantiate(PrefabWithUniqueThingOnIt);
 thingScript = thingObj.GetComponent<UniqueThing>();
 thingStript.SetHeldValue(false);
 Debug.Log(thingObj.GetComponent<UniqueThing>().ValueHeld()); //False, as expected
 Debug.Log(thingObj.GetComponent<BaseThing>().ValueHeld()); //True??
 Debug.Log(thingObj.GetComponent<ValueHolder>().ValueHeld()); //Also True??
 

The problem is that I want to have a single function that can get several variations of UniqueThing and check their ValueHeld. When I set the value on BaseThing and get it using ValueHolder, I do get the expected result, so I expect the problem is more related to inheritance than interfaces.

Comment
Add comment · Show 1
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 gregsaywhaat · Jun 21, 2019 at 05:18 AM 0
Share

The more I think about this, the more I think this is a Unity bug. I can't think of any good reason for this behavior. Isn't the exact point of interfaces to allow the thing I am trying to do?

1 Reply

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

Answer by Bunny83 · Jun 21, 2019 at 05:52 PM

This is certainly not a Unity bug. The most likely explanation for this is that you actually attached a UniqueThing as well as a BaseThing to the same object. GetComponent will return the first instance it finds on the object. When you do GetComponent<UniqueThing>() and there's only one UniqueThing component on the gameobject it will return that one. However GetComponent<BaseThing>() as well as GetComponent<ValueHolder>() would match both classes. So you probably have a "BaseThing" component besides the UniqueThing component attached.

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 gregsaywhaat · Jun 23, 2019 at 05:35 PM 0
Share

It's not 'certainly' anything yet.

I should not have both instances on the object. Earlier in the Doing some code things bit, I do AddComponent(UniqueThing) and call a function inside BaseThing that ends with Destroy(this). Is removing the component asynchronous?

avatar image Bunny83 gregsaywhaat · Jun 23, 2019 at 05:48 PM 1
Share

Yes, the destruction of objects is asynchronous. Try this code:

 void Start ()
 {
     var rb = gameObject.AddComponent<Rigidbody>();
     Destroy(rb);
     var test = GetComponent<Rigidbody>();
     if (rb == test)
         Debug.Log("Same");
 }

This will print "Same". If you use DestroyImmediate ins$$anonymous$$d of Destroy this would not happen. However you should use DestroyImmediate with care.

avatar image gregsaywhaat Bunny83 · Jun 24, 2019 at 01:08 AM 0
Share

Is there a good way to prevent GetComponent from finding destroyed components? I'm fine with doing the destruction at the end of the frame for the sake of efficiency, so I would say Getting destroyed components is the undesired behavior. DestroyImmediate sounds like it is a solid hit to speed (and is followed by a cloud of fearmongering), and I need to do this a lot, quickly.

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

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

"Re-usable" enemy behaviour? 2 Answers

Inheritance based state machine for controls? 0 Answers

Best way(s) to handle Custom Item Databases 1 Answer

Optional actions based on Interface Implementations 0 Answers

An OS design issue: File types associated with their appropriate programs 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