Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem getting gameObject from subclass

Discussion in 'Scripting' started by uk, Apr 9, 2013.

  1. uk

    uk

    Joined:
    Apr 8, 2013
    Posts:
    14
    this is my subclass:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DressItem : CreateModeC {
    5.  
    6.     void Start () {
    7.  
    8.         print(base.bodyParts);
    9.  
    10.     }
    11.    
    12.     void Update () {
    13.    
    14.     }
    15. }
    If I print this using print(bodyParts) in my parent class it actually returns the gameobject, while in the subclass it returns null. Any ideas?
     
  2. meganaut

    meganaut

    Joined:
    Mar 8, 2012
    Posts:
    18
    Where is bodyParts initialized? is it somewhere that will be inherited by the subclass? If it's a a public variable.... just use print(bodyParts);
     
  3. uk

    uk

    Joined:
    Apr 8, 2013
    Posts:
    14
    Thanks for the reply!

    alright, Il try to show this a bit better, heres a base class:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CreateModeC : MonoBehaviour {
    5.    
    6.     public GameObject emptyGameObject;
    7.     public int someInt = 12;
    8.    
    9.     void Start () {
    10.         emptyGameObject = GameObject.Find("emptyGObject");
    11.         gameObject.AddComponent("DressItem");
    12.         print(emptyGameObject);
    13.     }
    14. }
    and heres a subclass:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DressItem : CreateModeC {
    5.  
    6.     void Start () {
    7.         print(someInt);
    8.                 print(emptyGameObject);
    9.     }
    10. }
    now, in the base class it will print emptyGameObject, in the subclass however, it will print someInt just fine. But not emptyGameObject, wich will print as: null. Why does it give diffrent results depending if its printing from sub or base? while the int prints just fine in both.
     
  4. AlteredReality

    AlteredReality

    Joined:
    Aug 15, 2011
    Posts:
    397
    The reason for this is because the code in the base class is not called. It was hidden by the "void Start()" function in the sub class. If you want to call both versions of Start(), you'd need to make it virtual, like this...

    In the base class
    Code (csharp):
    1.  
    2.  virtual void Start() {
    3.     ... //base functions code
    4.  }
    5.  
    In the sub class...
    Code (csharp):
    1.  
    2.  override void Start() {
    3.     base.Start(); //call the base classes version of start
    4.     ... //rest of your sub class start code
    5.  }
    6.  
    Let me know if that helps!
     
  5. uk

    uk

    Joined:
    Apr 8, 2013
    Posts:
    14
    thanks for the reply!

    I did try to put virtual and override as you told, and base.Start(); in my subclass

    but I get the following error: (10,22): error CS0621: `CreateModeC.Start()': virtual or abstract members cannot be private

    And im just confused by what that means, none of my code has been set to private?...

    Edit:

    I tried to put: public virtual void Start () on both base and subclass, now it does not give me an error, but it prints every frame. wich somehow makes sense since the subclass is calling base.Start();...So it does some kind of endless loops of starts....but hey, I dont know... mostly confused right now hehe :)
     
    Last edited: Apr 10, 2013
  6. AlteredReality

    AlteredReality

    Joined:
    Aug 15, 2011
    Posts:
    397
    Oh yeah, my bad, you definitely need to make the public. (functions are private by default)

    I believe the only reasons the start function should be called is because an object just started on a load, you created a new object that was started, or you are directly calling it. Are any of these the case?