Referencing Actors and fields in custom script

Announcements, support questions, and discussion for the Dialogue System.
User avatar
Tony Li
Posts: 21987
Joined: Thu Jul 18, 2013 1:27 pm

Re: Referencing Actors and fields in custom script

Post by Tony Li »

If you step forward once more so that the yellow line is on the "if" statement, is talkLvl updated to1?
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

Re: Referencing Actors and fields in custom script

Post by annathehank »

Well....my Unity had a lil conniption fit so I had to force quit and load back up.
And now when I play it it works perfectly...

I'll keep you updated but hopefully, it was just a weird glitch?
User avatar
Tony Li
Posts: 21987
Joined: Thu Jul 18, 2013 1:27 pm

Re: Referencing Actors and fields in custom script

Post by Tony Li »

It might've just been Unity bugging out. Anyway, glad it's working now!
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

Re: Referencing Actors and fields in custom script

Post by annathehank »

Me too! Thank you very much for the help <3
User avatar
Tony Li
Posts: 21987
Joined: Thu Jul 18, 2013 1:27 pm

Re: Referencing Actors and fields in custom script

Post by Tony Li »

Glad to help!
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

Re: Referencing Actors and fields in custom script

Post by annathehank »

Hi, hope it's okay to revive this again, as I still don't think it's working properly even after playing around with it a bit more.

It sometimes does and sometimes doesn't show the alert. And when I run it as it is I get the Nullreferenceobject alert for this line

Code: Select all

foreach (Actor NPC in DialogueManager.masterDatabase.actors)
Here's what the code looks like right now:

Code: Select all

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

public class charalert : MonoBehaviour
{//Alert notification to display on map screen
    public GameObject AlertNotif;

    void Update()
    {
      
        //cycling through each actor component)
        foreach (Actor NPC in DialogueManager.masterDatabase.actors)
        {//Look to see if the level of relationship matches the conversation level player is on
            int RelLvl = DialogueLua.GetActorField(NPC.Name, "RelLvl").asInt;
            int TalkLvl = DialogueLua.GetActorField(NPC.Name, "TalkLvl").asInt;
            if ((TalkLvl*10)>= RelLvl)
            {//display altert notification on map screen
               Transform actorTransform = PixelCrushers.DialogueSystem.CharacterInfo.GetRegisteredActorTransform(NPC.Name);
                if (actorTransform != null)
                {
                    actorTransform.GetComponent<charalert>().AlertNotif.SetActive(true);
                }
               
            }
        }
    }

    // Update is called once per frame
    
}
I'm wondering if it's the name of the variable being used for the Actors?
User avatar
Tony Li
Posts: 21987
Joined: Thu Jul 18, 2013 1:27 pm

Re: Referencing Actors and fields in custom script

Post by Tony Li »

Hi,

Do you see just one NullReferenceException error or multiple?

After these 2 lines:

Code: Select all

if ((TalkLvl*10)>= RelLvl)
{//display alert notification on map screen
try adding this extra line:

Code: Select all

[code]if ((TalkLvl*10)>= RelLvl)
{//display alert notification on map screen
    Debug.Log("About to show alert notification for " + NPC.Name); //<--ADD THIS
Does the Console window consistently show that Debug.Log message when it's supposed to?

If so, then the issue is below that line. Maybe it's not finding the GameObject.

If it doesn't show that Debug.Log message, then we can look at the field values.
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

Re: Referencing Actors and fields in custom script

Post by annathehank »

I never see a log for that. It just constantly generates this error

NullReferenceException: Object reference not set to an instance of an object
charalert.Update () (at Assets/Scripts/charalert.cs:14)

And when I click on them they cycle through the npc assets loaded into the scene(each of which has the script on them to allow me to add the alert notif image for them in the right place.
annathehank
Posts: 95
Joined: Sun May 03, 2020 2:17 pm

Re: Referencing Actors and fields in custom script

Post by annathehank »

Strike that.
I do see the log for those (I had the dialogue editor turned off when I was checking this time around. But it still seems like the alerts are not working, I get a lot of about to show alert notifs for each npc but nothing else.)
For example, when I load up the scene, all of the values should be set to match, yet only one pops up.
User avatar
Tony Li
Posts: 21987
Joined: Thu Jul 18, 2013 1:27 pm

Re: Referencing Actors and fields in custom script

Post by Tony Li »

Hi,

So you see "About to show alert notification for X" in the Console every time you expect, but the NPC's AlertNotif doesn't always become active when it should?

If that's the case, add these 2lines:

Code: Select all

if (actorTransform != null)
{
    Debug.Log("About to set AlertNotif active on " + actorTransform, actorTransform); //<--ADD
    actorTransform.GetComponent<charalert>().AlertNotif.SetActive(true);
}
else
{
    Debug.LogWarning("There is no registered transform for the actor named " + NPC.Name); //<--ADD
}
Then reproduce the issue.
  • If you see "About to set AlertNotif active on X" but you don't see the AlertNotif, then maybe something in your charalert script is immediately deactivating or, or maybe it's not visible somehow (e.g., renderer disabled) even though the GameObject is active.
  • If you see "There is no registered transform..." then at the point this code has run, no Dialogue Actor component has registered itself with the Dialogue System under the specified actor name.
Also, feel free at any point to send a reproduction project to tony (at) pixelcrushers.com if you like. I'll be happy to take a look.
Post Reply