Actor Reference Trouble (again)

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

Actor Reference Trouble (again)

Post by annathehank »

Hi there,

Once more having some trouble referencing actors properly. I'm creating a situation in which the player has to assign NPC's to the right seats via a click and drag mini game.
I'm tyring to figure out how to get it so that the script knows when the dragged object is being placed in the right spot. I have given all the NPC items being dragged actor components, but whenever I run this it doesn't seem to work.

Here's the script that's on the seats

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using PixelCrushers.DialogueSystem;

public class dropitem : MonoBehaviour, IDropHandler
{
    public string check;
    public bool correct;
    private Dragdrop dragDrop;
     void Awake()
    {
        dragDrop = GetComponent<Dragdrop>();
    }
    
    public void OnDrop(PointerEventData eventData)
    {
       
            
            if ( dragDrop.name == check)
            {
                correct = true;
            }

            Debug.Log("dropped");
        if (eventData.pointerDrag != null)
        {
            eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
           
        }
      
     
    }
}
    



And here's the code on each of the draggable NPCs

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using PixelCrushers.DialogueSystem;

public class Dragdrop : MonoBehaviour, IPointerDownHandler, IBeginDragHandler, IEndDragHandler, IDragHandler, IDropHandler
{
   
    private RectTransform rectTransform;
    private CanvasGroup canvasGroup;
    
    void start()
    {
        foreach (Actor NPC in DialogueManager.masterDatabase.actors)
        {
            string name = DialogueLua.GetActorField(NPC.Name, "Name").asString;
            Transform actorTransform = PixelCrushers.DialogueSystem.CharacterInfo.GetRegisteredActorTransform(NPC.Name);

        }
    }
    private void Awake()
    {
        rectTransform = GetComponent<RectTransform>();
        canvasGroup = GetComponent<CanvasGroup>();
   
    }
    public void OnBeginDrag(PointerEventData eventData)
    {
        Debug.Log("drag");
        canvasGroup.blocksRaycasts = false;
    }

    public void OnDrag(PointerEventData eventData)
    {
        Debug.Log("isdraggin");
        rectTransform.anchoredPosition += eventData.delta;
    }
    
    public void OnEndDrag(PointerEventData eventData)
    {
        Debug.Log("no drag");
        canvasGroup.blocksRaycasts = true;
    }
 public void OnPointerDown(PointerEventData eventData)
    {
        Debug.Log("pointdown");
    }
    
    public void OnDrop(PointerEventData eventData)
    {

    }
}

When I run it, I get this error when I drop the NPC on a seat
NullReferenceException: Object reference not set to an instance of an object
dropitem.OnDrop (UnityEngine.EventSystems.PointerEventData eventData) (at Assets/scripts/dropitem.cs:21)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IDropHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/Program Files/Unity/Hub/Editor/2019.4.0f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:85)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/Program Files/Unity/Hub/Editor/2019.4.0f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update() (at C:/Program Files/Unity/Hub/Editor/2019.4.0f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:377)
Yet without any of the dialogue manager stuff added, the snap in place function was working and the consol logged it was dropped.
Any idea what's causing the kerfuffle? Thanks in advance <3
User avatar
Tony Li
Posts: 21961
Joined: Thu Jul 18, 2013 1:27 pm

Re: Actor Reference Trouble (again)

Post by Tony Li »

Hi,

I'm not sure what this bit of dropitem.cs is supposed to do:

Code: Select all

if ( dragDrop.name == check)
{
    correct = true;
}
but it's throwing an error on this line:

Code: Select all

if ( dragDrop.name == check)
which suggests that dragDrop is null.

Also, I don't think Dragdrop.cs's start() method does anything. You can probably remove it. Side note: If you intended it to run automatically when the GameObject starts, it should be named Start() with a capital 'S'.
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

Re: Actor Reference Trouble (again)

Post by annathehank »

For this

Code: Select all

if ( dragDrop.name == check)
{
    correct = true;
}
I have the string variable called 'check' assigned to each seat that is filled in with one of the actors' name to indicate that it is their seat. Such that, theoretically, when a game object with the dialogue actor component is dragged onto the seat with the same same actor name filled into the 'check' field the 'correct' bool will become true, letting the game know that the right NPC is in the right seat.
Is there a different/better way to do that?
User avatar
Tony Li
Posts: 21961
Joined: Thu Jul 18, 2013 1:27 pm

Re: Actor Reference Trouble (again)

Post by Tony Li »

Try removing Dragdrop's start() method (since it doesn't do anything), and change dropitem's OnDrop() method to something like:

Code: Select all

public void OnDrop(PointerEventData eventData)
{
    if (eventData.pointerDrag != null)
    {
        GameObject draggedObject = eventData.pointerDrag;
        
        // Move dragged object to this slot's position:
        draggedObject.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
    
        // Record whether it's the correct actor or not:
        correct = (draggedObject.name == check);
    }
}
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

Re: Actor Reference Trouble (again)

Post by annathehank »

Just to clarify, since I've never seen it before

Code: Select all

correct = (draggedObject.name == check)
Does this syntax mean that the bool will set to true if the statement inside the parenthesis is true? And set to false if not?

Thank you for all the help again! I really appreciate your willingness to help new coders <3
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

Re: Actor Reference Trouble (again)

Post by annathehank »

Oh, and it is working perfectly by the way!! Thank you so much <3
User avatar
Tony Li
Posts: 21961
Joined: Thu Jul 18, 2013 1:27 pm

Re: Actor Reference Trouble (again)

Post by Tony Li »

Glad to help! :-)
Post Reply