2
votes

I am using Unity3D to create a demo. The demo is like this: Use the EasyTouch to control the movement of the gameobject named "Player" —— the plane.

After creating the EasyTouch, I create a C# script like this:

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {
    public float m_speed = 1;

    protected Transform m_transform;

    public MovePlayer m_movePlayer;
    // Use this for initialization
    void Start () {
        m_transform = this.transform;
    }

    // Update is called once per frame
    void Update () {
    }

    void OnEnable(){
        Debug.Log ("OnEnable");
        EasyJoystick.On_JoystickMoveStart += HandleOn_JoystickMoveStart;
        EasyJoystick.On_JoystickMove += HandleOn_JoystickMove;
        EasyJoystick.On_JoystickMoveEnd += HandleOn_JoystickMoveEnd;
    }

    void HandleOn_JoystickMoveStart (MovingJoystick move)
    {

    }

    void HandleOn_JoystickMoveEnd (MovingJoystick move)
    {
    }

    void HandleOn_JoystickMove (MovingJoystick move)
    {
        Debug.Log ("HandleOn_JoystickMove");

        if (m_transform == null) {
                m_transform = this.transform;
        }

        if (move.joystickName != "moveJoystick") {
            Debug.Log ("return");
            return;
        }

        float currentPositionX = this.gameObject.GetComponent<Transform> ().position.x;
        float currentPositionZ = this.gameObject.GetComponent<Transform> ().position.z;

        float joyPositionX = move.joystickAxis.x;
        float joyPositionY = move.joystickAxis.y;
        Debug.Log ("joyPositionX = " + joyPositionX + " joyPositionY = " + joyPositionY);

        float moveh = joyPositionX / 10;
        float movev = joyPositionY / 10;

        this.m_transform.Translate (new Vector3 (-joyPositionX/10, 0, -joyPositionY/10));
    }
}

When I start the game, the code worked fine, and the plane will be control.

Then I create the button by GUI.

When the button was clicked, the code Application.LoadLevel(0) will be called.And the level will restart.

But after I called Application.LoadLevel(0) to restart the game. The plane will can not be control by the EasyTouch, and the Error message will appear:

MissingReferenceException: The object of type 'Player' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.

I wonder at why the "Player" will be null. The "Player" should be init after called Application.LoadLevel(0), doesn't it?

1

1 Answers

2
votes

Preamble

I have never used this EasyTouch plugin (fortunately? :D). So I'll make an assumptions on what I see in your code. If I'm wrong — sorry :)

Answer

Take a look on this code:

void OnEnable(){
    Debug.Log ("OnEnable");
    EasyJoystick.On_JoystickMoveStart += HandleOn_JoystickMoveStart;
    EasyJoystick.On_JoystickMove += HandleOn_JoystickMove;
    EasyJoystick.On_JoystickMoveEnd += HandleOn_JoystickMoveEnd;
}

It looks like EasyJoystick is a static class. And you are subscribing to static events here. Then after calling Application.LoadLevel() the instance of your Player class marked as destroyed. But subscriptions don't go anywhere and once EasyJoystick fires one of the events (On_JoystickMoveStart, On_JoystickMove or On_JoystickMoveEnd) corresponding methods (HandleOn_JoystickMoveStart, HandleOn_JoystickMove or HandleOn_JoystickMoveEnd) are being invoked. This is why you get the exception. And it is probably thrown by this line (am I wrong? :)):

float currentPositionX = this.gameObject.GetComponent<Transform> ().position.x;

In order to fix the problem you need to unsubscribe Player instance(s) from the events mentioned before invoking Application.LoadLevel().

Side note

This:

float currentPositionX = this.gameObject.GetComponent<Transform> ().position.x;

can be rewritten as:

float currentPositionX = transform.position.x;

Though currentPositionX and currentPositionY are not used at all :D

Edit. How to unsubscribe

Something like this:

void OnDisable(){
    Debug.Log ("OnDisable");
    EasyJoystick.On_JoystickMoveStart –= HandleOn_JoystickMoveStart;
    EasyJoystick.On_JoystickMove –= HandleOn_JoystickMove;
    EasyJoystick.On_JoystickMoveEnd –= HandleOn_JoystickMoveEnd;
}