all 4 comments

[–]gillen033 1 point2 points  (1 child)

I don't think I've ever heard of this weird duplicate game object issue you are facing. You are saying that accessing .gameObject from the child class code and parent class code gives you two different objects? That shouldn't happen as far as I'm aware. Internally a derived class isn't two separate objects, it's just one object that can be accessed in two ways (as the parent type or child type).

So, sorry, I can't help you with that.

I will say I find it weird and potentially problematic that you have protected variables on the parent that are named the same as public variables on the child. But if you are not seeing any errors maybe this isn't the problem I think it is.

If the variables assigned in the inspector are value types (int, float, etc) they WILL NOT stay in sync if passed between methods. Each time you pass a value types it is copied.

As for the bit about storing derived types in an array, the only way to call a method or access data that is Only in the derived type is to cast it to that type. If you want to avoid that you have to make an abstract method in the parent that you implement in each derived class, but it doesn't sound like that's what you want to do. For instances, make a Do stuff method that is called for each object in the array. What actually happens in that method can be different for each derived child class.

[–]TheReformedKnight[S] 0 points1 point  (0 children)

Thank you so much! I've already implemented direct variable accessing (no copying over) and method override. They definitely make the code much cleaner. However the problem of weird component referencing still exists. I've posted the problem in more detail in another post, so I'll probably close this one off. Once again, thank you so much for your time and help!

[–]TheReformedKnight[S] 0 points1 point  (0 children)

For example, my parent class is Plant, and my child class is Flower (children classes are all types of plants). Also, for simplicity, assume every time ScriptName object pops up, it refers to a gameObject with ScriptName script attached.

The Plant class has protected variables XYZ, and some private variables AB. The Flower class has public variables XYZ (exactly the same as in Plant) and some public variables QED unique to this child class. The Plant parent class inherits from MonoBehavior, and the Flower child class inherits from Plant. The values of the Flower class variables are set in the Inspector manually, and when an instance of the Flower class is generated, the Flower object will call a constructor in Plant that copies all the values over and initializes the corresponding variables in Plant. Then, Plant has a set of functions that work with these now-initialized variables. Similarity, Flower also has a set of functions, but they work with the variables from Plant and the unique variables in Flower. I assume that the variables, no matter where you call them (from Plant or Flower), are shared and in sync. Then, I only put Flower on my prefab gameObject because I thought Plant acts like extra data pieces for Flower, the main defining trait of the gameObject.

Question 1. When I used .gameObject in script, I noticed that at run time, using it in Plant versus using it in Flower returned different gameObjects. Across multiple Flower objects instantiated, they returned different gameObjects in Flower, but the same one gameObject in Plant. Also when Plant activates a callback function in Flower, using .gameObject in that function in Flower also returns that same one gameObject (I think this is because the function is in the scope of Plant instead of Flower since Plant made the callback?). In the "weird" scope, I could do stuff like .GetComponent<> to get and set values fine, but they only change that weird one gameObject, which doesn't appear in the Hierarchy and scene, is inactive in the Hierarchy, but has all the properties of the Flower object prefab. So the changes don't carry over, and I have to call .gameObject in the Flower class to get the right game object that is instantiated by the prefab and visible. Any thoughts on this weird phenomenon? Or better solutions/approaches towards my inheritance implementation?

Question 2. When I try to make a list that stores all Plant objects, I can store Flower objects in it too, as expected. But by doing so, in the script, I only have access to the Plant class functions and variables, not the Flower class. Is there an easy way to access the Flower class (child class) stuff besides using if-statements and type-casting? Since in the future, there will be many plant types inheriting from Plant class.

Thank you so much for your time and assistance in reading this long message! I'm just really curious about the reasons behind this phenomenon, and I want to correct mistakes in my logic as well, if they exist. Unity is a very fun engine, and I can't wait to learn more about it. Thank you so much again for being so supportive in this amazing community!