Thursday, October 27, 2022
HomeGame Developmentunity - Getting a null reference when utilizing 2 delegates in a...

unity – Getting a null reference when utilizing 2 delegates in a category


I’ve a category that’s an occasion supervisor, which is notifying different scripts of contact occasions. Nonetheless, the issue is that whereas the primary public static occasion – SendInteraction – works, the second occasion, SendDrag, doesn’t appear to be sending the place to the subscribed class. As an alternative, it simply prints a null reference error.

“NullReferenceException: Object reference not set to an occasion of an object
TouchScreenManager.Replace () (at ../TouchScreenManager.cs:47)”

I’ve tried making the TouchScreenManager class a Singleton and eradicating references to the occasions as static, and as an alternative creating new situations in every subscribed class in Awake() however that does not work both. How can I repair it?


public class TouchScreenManager : MonoBehaviour
  { 
    #area occasions
    public delegate void SendInteraction(GameObject merchandise);
    public static occasion SendInteraction MoveScene;

    public delegate void SendDrag(Vector2 place);
    public static occasion SendDrag Drag;
    #endregion

    personal Vector3 startPos;
    personal Vector2 startPosition;
    personal WaitForFixedUpdate waitForFixedUpdate = new WaitForFixedUpdate();

    void Awake()
    {
        EnhancedTouchSupport.Allow();
        TouchSimulation.Allow();
    }

    void Replace()
    {
        if (Contact.activeFingers.Depend == 1)
        {
            var contact = Contact.activeTouches[0];

            if (contact.section == TouchPhase.Started)
            {
                startPosition = contact.startScreenPosition;
                startPos = Digital camera.essential.ScreenToWorldPoint(contact.screenPosition);
                DragAndDrop(contact);
                CheckForInteraction(contact);
            }

            else if (contact.section == TouchPhase.Moved)
            {
                Debug.Log("from the contact display screen supervisor:" + contact.screenPosition);
                Drag(contact.screenPosition); // PRINTS FINE HERE
            }
        }
    }

This script is subscribed to this occasion.

public class LineGenerator : MonoBehaviour
{
    public GameObject linePrefab;
    Line activeLine;
    Vector2 capturedScreenPos;

    personal void OnEnable()
    {
        TouchScreenManager.Drag += DrawLine;
    }

    personal void OnDisable()
    {
        TouchScreenManager.Drag -= CompleteLine;
        
    }

    void DrawLine(Vector2 place)
    {
        GameObject newLine = Instantiate(linePrefab);
        activeLine = newLine.GetComponent<Line>();
        capturedScreenPos = place;
        Debug.Log("from the road generator: " + place); // NULL REF HERE
    }
}
````

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments