News: Enjoy our Christmas offer 30% off on our latest package Tower Defense Level Kit http://u3d.as/m7h

  • September 24, 2018, 03:10:40 PM

Login with username, password and session length

Author Topic: OnEnable causes Null Reference.  (Read 2021 times)

Offline xyzDave

  • Newbie
  • *
  • Posts: 9
OnEnable causes Null Reference.
« on: December 09, 2015, 09:55:45 PM »
Hi folks,
love the library.
Run into a slight problem , dropping it into a unity app I'm doing (I'm replacing a different animation system).
The problem is the OnEnable is used to start the animations (the gameobject is enabled and disabled to hide/reveal it when required, standard stuff I think), and this crashes with a Null Reference in ComputePosition.
The problem seems to be caused by cachedTransform being Null.
I've seen issues before as Start() is called AFTER OnEnable(), so this OnEnable() will call Open() before the Start() function in EMCloseOpenMotion.

Any preferred  method or suggestions on how to fix / code around? 

I'm using Unity 5.3 if that makes any difference?

Thanks
Dave

Stack trace:
Code: [Select]
EMBaseMotion.ComputePosition (anchoredPosition={(0.0, 0.0, 0.0)}, direction=Debuggee returned error code ERR_UNLOADED., scale={(0.0, 0.0, 0.0)}, inside=false) in S:\e3ct\AML-Game-V1\Assets\EasyUIMotion\Plugins\EMBaseMotion.cs:312
EMBaseMotion.GetTransformFromProperties (prop={EMMotionProperties}, useCurrent=false) in S:\e3ct\AML-Game-V1\Assets\EasyUIMotion\Plugins\EMBaseMotion.cs:368
EMOpenCloseMotion.Open () in S:\e3ct\AML-Game-V1\Assets\EasyUIMotion\Plugins\EMOpenCloseMotion.cs:92
knowdontknow.OnEnable () in S:\e3ct\AML-Game-V1\Assets\Old_Scripts\knowdontknow.cs:21

The code is as follows:
Code: [Select]
public class knowdontknow : MonoBehaviour {

    EMOpenCloseMotion emocm;
    void Awake()
    {
        emocm = GetComponent<EMOpenCloseMotion>();

    }
    void Start()
    {
        emocm.Open();
    }

void OnEnable()
{
            if (emocm != null)
           {
               emocm.Open();
           }
       }
}
« Last Edit: December 09, 2015, 10:03:29 PM by xyzDave »

Offline Nicolas

  • Administrator
  • Hero Member
  • *****
  • Posts: 792
Re: OnEnable causes Null Reference.
« Reply #1 on: December 10, 2015, 06:18:41 PM »
Hi,

This problem comes from the execution order of the script. Let me explain, Unity handles callback in a given order script by script:

Example with 2 scripts A & B , Unity sequence :

Awake => Script A
Enable => Script A

Awake => Script B
Enable => Script B

Start => Script A
Start => Script B

As you can see Unity operat Awake & Enable in the same loop, before to passe to another script.In your case  script A = "your script" & B = "EMMotion", that mean that you request the opening prior to initiation of movement.

To prevent this, you must wait the end of frame, to ask the opening like this.

Code: [Select]
EMOpenCloseMotion emocm;

void Awake(){
emocm = GetComponent<EMOpenCloseMotion>();
}

void Start(){
emocm.Open();
}

void OnEnable(){
StartCoroutine("LaunchOpen");
}

IEnumerator LaunchOpen(){
yield return new WaitForEndOfFrame();
emocm.Open();
}



Offline xyzDave

  • Newbie
  • *
  • Posts: 9
Re: OnEnable causes Null Reference.
« Reply #2 on: December 10, 2015, 09:57:00 PM »
Hi,
I've been playing around with this some more since I reported the issue.

I was wondering if it might be an idea to have an "isInitialised" function to determine if the library had been initialised.

e.g.
  public bool isInitialised()
    {
        if (cachedCanvasScaler!=null)
            return true;
       
        return false;
    }

And then checking it in open()
e.g.

public void Open()
{
     if (!isInitialised())
         return;
     if (motionState == MotionState.Closed){
        GetTransformFromProperties( openProp,false).ApplyAllToRectTransform( cachedTransform, cachedCanvasGroup);
         currentProp = openProp;
         LaunchMotionSequence( openProp,false,true);
      }
}

Is this worth adding to your library code (I don't want to do it locally in case I get an update and loose the changes, and get a bit confused over what has gone wrong!)

Another alternative is to set a bool in the Start() function, which I check in the OnEnable()
If not set, skip the Open command.
That works too.

I've used the co-routine method before with a different library, but was looking for a more elegant solution.

Cheers,
Dave



Offline Nicolas

  • Administrator
  • Hero Member
  • *****
  • Posts: 792
Re: OnEnable causes Null Reference.
« Reply #3 on: December 12, 2015, 10:40:16 AM »
Hi,

If you do not use the coroutine, it will be up to you to run a loop to wait for activation.

Honestly I prefer to present this code than adding a loop with boolean in the update.