Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Setter not working in parent class

Discussion in 'Scripting' started by wamballa, Dec 17, 2018.

  1. wamballa

    wamballa

    Joined:
    Jun 2, 2018
    Posts:
    19
    Hi there,

    I'm having a problem figuring out changing a variable in a parent class.

    MiddlePartManager class inherits from ShipPart class
    MiddlePartManager is initiated elsewhere by method Init()
    Init() calls a setter in the parent ShipPart that is called IsInitiated (allows the parent's Update loop to start doing stuff)
    and this setter should set a bool in the parent - isInitiated - to True

    However this does work. The bool in the parent class always remains False ( unless in the setter I explicitly type isInitiated = True )

    I can see when the IsInitiated setter is called that the isInitiated private bool is true using print statements.
    But when I check in it's FixedUpdate loop (using keypress X) I can see that the the isInitiated bool has not been set to True.

    What am I doing wrong?

    Thanks Steve

    Code (CSharp):
    1.  
    2.  
    3. public class MiddlePartManager : ShipPart {
    4.  
    5.     // Init() is entry point called from elsewhere and should initiate the parent class to do stuff
    6.     {
    7.         print("PART - MIDDLE - INIT");
    8.         IsInitiated = true;  // triggered externally, this should tell the parent to do stuff in it's update loop
    9.     }
    10.  
    11.      void Start () {
    12.         print("PART - MIDDLE - START "+ IsInitiated);
    13.         IsInitiated = false; // sets false until triggered elsewhere
    14.     }
    15.  
    16.     private void FixedUpdate()
    17.     {
    18.         base.FixedUpdate();
    19.     }
    20.  
    21. }
    Code (CSharp):
    1.  
    2.  
    3. public class ShipPart : MonoBehaviour
    4. private bool isInitiated;
    5.  
    6.     protected bool IsInitiated
    7.     {
    8.         get { return isInitiated; }
    9.         set
    10.         {
    11.             print("isInitiated set to value of " + value);
    12.             isInitiated = value;
    13.             print("isInitiated set to value of " + isInitiated);
    14.         }
    15.     }
    16.  
    17.     protected void FixedUpdate()
    18.     {
    19.         if (Input.GetKey(KeyCode.X))
    20.         {
    21.             print("IS INITIATED " + IsInitiated); // Always returns False
    22.         }
    23. }
    24. }
    25.  
    26. {
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    I assume you verify that Start() is getting called before Init()? I see you have debug log statements...

    In the console window you can actually look down and see the call stack for each entry (select the line) and see who called it. That might give you a clue to what is going on.
     
  3. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    Where in your code are you changing it to true after the start method?
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,991
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TestScript2 : MonoBehaviour
    4. {
    5.     private bool isInitiated;
    6.  
    7.     protected bool IsInitiated
    8.     {
    9.         get { return isInitiated; }
    10.         set
    11.         {
    12.             Debug.Log("isInitiated set to value of " + value);
    13.             isInitiated = value;
    14.             Debug.Log("isInitiated set to value of " + isInitiated);
    15.         }
    16.     }
    17.  
    18.     protected void Update()
    19.     {
    20.         if(Input.GetKey(KeyCode.X))
    21.         {
    22.             Debug.Log("IS INITIATED " + IsInitiated); // Always returns False
    23.         }
    24.     }
    25. }
    26.  
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TestScript : TestScript2
    4. {
    5.     void Init() //is entry point called from elsewhere and should initiate the parent class to do stuff
    6.     {
    7.         Debug.Log("PART - MIDDLE - INIT");
    8.         IsInitiated = true;  // triggered externally, this should tell the parent to do stuff in it's update loop
    9.     }
    10.  
    11.     void Start()
    12.     {
    13.         Debug.Log("PART - MIDDLE - START " + IsInitiated);
    14.         IsInitiated = false; // sets false until triggered elsewhere
    15.     }
    16.  
    17.     protected new void Update()
    18.     {
    19.         base.Update();
    20.         if(Input.GetKeyUp(KeyCode.Z))
    21.         {
    22.             Init();
    23.         }
    24.     }
    25. }
    26.  
    This setup is working for me. (Obviously only the TestScript was attached to a GO)
     
    Last edited: Dec 17, 2018
    DeeJayVee likes this.
  5. wamballa

    wamballa

    Joined:
    Jun 2, 2018
    Posts:
    19
    After the start method has set isInitiated to false the object waits until it's triggered by a call to it's Init method where it's set to true
     
  6. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    is it because your Init() is //commented out?
     
  7. wamballa

    wamballa

    Joined:
    Jun 2, 2018
    Posts:
    19
    Sorry DeeJayVee...in the code it actually reads:
    public void Init()
     
  8. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    All good, I was just curious myself.
     
  9. wamballa

    wamballa

    Joined:
    Jun 2, 2018
    Posts:
    19
    Hmmm just created a test object too and used your code. It worked for me too!? Looks like I have to some debugging...[/QUOTE]
     
  10. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,991
    You probably don't call the Init() properly.
     
  11. wamballa

    wamballa

    Joined:
    Jun 2, 2018
    Posts:
    19
    I've attached a screenshot of the debug logs and can see that Init() is called.

    When i go back into the parent class and inside the setter hardcode isInitiated = True...everything works

    Code (CSharp):
    1.     protected virtual bool IsInitiated
    2.     {
    3.         get { return isInitiated; }
    4.         set
    5.         {
    6.             print("isInitiated set to value of " + value);
    7.             isInitiated = value;
    8.             isInitiated = true;
    9.             print("isInitiated set to value of " + isInitiated);
    10.         }
    11.     }
     

    Attached Files:

  12. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Check the call order for Start and Init. Start doesn't get called until the first frame a GameObject is active. Which can be quite a long time if you Instantiate things inactive.
     
  13. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,991
    We can guess more, but the problem is not with this code I think. So your problem is somewhere else. Or just replace these with my code and rename them to your class names and see that your problem is still alive.
     
  14. wamballa

    wamballa

    Joined:
    Jun 2, 2018
    Posts:
    19
    Ok. I'll do that now
     
  15. wamballa

    wamballa

    Joined:
    Jun 2, 2018
    Posts:
    19
    Thanks for all your help. It's now fixed.
    I'm ashamed to say I was calling the Init() method incorrectly. Thank you LurkingNinjaDev and everyone else for helpful tips!!
     
    Kurt-Dekker and Lurking-Ninja like this.