Trying to access a C# script's void on an answer in a dialogue with Events

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
xDonnervogelx
Posts: 2
Joined: Mon Oct 09, 2023 9:11 am

Trying to access a C# script's void on an answer in a dialogue with Events

Post by xDonnervogelx »

Hi,
I am totally new to this asset. Watched some tutorials and followed the initial one. Unity version 2021.3.30f1.
I wanted to go a step further and actually influence something in my scene, change a material by script when I click a specific answer.

the script:

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MaterialSwitcher : MonoBehaviour
{
    public GameObject materialGO;
    private Material currentMaterial;
    public Material newMaterial;    

    public void ReplaceMaterial()
    {
        Renderer renderer = materialGO.GetComponent<Renderer>();
        Material currentMaterial = renderer.material;
        currentMaterial = newMaterial;
    }
}
I tried to attach my script to the Event of the answer I chose but it can't find the void (I set it to public).
I also cannot drag and drop the game object that holds the script and the input in the inspector that I need... what the flip? Why?
It just finds the string name??
Unbenannt.PNG
Unbenannt.PNG (62.49 KiB) Viewed 327 times
I don't know what to do now, is my code wrong (given my intellisense is not working correctly in two programs and idk why), do I use the wrong component, does it not read my script... If somebody were so kind to help this confused beginner out, I'd be very grateful. Pointing me to a relevant video is also okay. I just don't know what's going on.

Thanks in advance for any help.
Flying too close to the sun? No problem, just a slight inconvenience.
suisei
Posts: 10
Joined: Tue Aug 22, 2023 7:45 am

Re: Trying to access a C# script's void on an answer in a dialogue with Events

Post by suisei »

I think you should use custom Lua function: https://www.pixelcrushers.com/dialogue_ ... erFunction (there is also a link to the video tutorial)
I tried to attach my script to the Event of the answer I chose but it can't find the void (I set it to public).
I also cannot drag and drop the game object that holds the script and the input in the inspector that I need... what the flip? Why?
Scene-Independent Event has tooltip on hover:
This UnityEvent cannot point to scene objects. It can point assets and prefabs.
User avatar
Tony Li
Posts: 22110
Joined: Thu Jul 18, 2013 1:27 pm

Re: Trying to access a C# script's void on an answer in a dialogue with Events

Post by Tony Li »

Hi,

If your script is attached to a GameObject in the scene, click the Events > Add Scene Event button. This will add a UnityEvent that can accept scene objects. Assign the GameObject. Then from the dropdown select your script (MaterialSwitcher) and the method you want to invoke.
xDonnervogelx
Posts: 2
Joined: Mon Oct 09, 2023 9:11 am

Re: Trying to access a C# script's void on an answer in a dialogue with Events

Post by xDonnervogelx »

Thanks a lot for the fast and detailed replies! Very grateful.

I see, so I have to press the button below the events list. I managed to add the script now, wow!
Hope I'll figure out the rest, if not I hope I find a solution in here or the docs.
Unbenannt.PNG
Unbenannt.PNG (16.36 KiB) Viewed 300 times
Flying too close to the sun? No problem, just a slight inconvenience.
User avatar
Tony Li
Posts: 22110
Joined: Thu Jul 18, 2013 1:27 pm

Re: Trying to access a C# script's void on an answer in a dialogue with Events

Post by Tony Li »

Glad to help!
Post Reply