#make a thread I'll explain it

1 messages · Page 1 of 1 (latest)

whole cove
#

hi

gray minnow
#

Ok so basically when you do say public or [SerializeField] private UnityEvent myEvent

#

now you see in the inspector

whole cove
#

right

gray minnow
#

there is the same thing as the InputField?

whole cove
#

wdym

gray minnow
#

when you add UnityEvent

#

to script

#

and save

#

look at inspector

whole cove
#

hold on my inspector is frozen

gray minnow
#

alr

whole cove
#

okay back

gray minnow
#

you did it?

whole cove
#

I'm trying to find it one sec

#

yeah it's set in the insepector

gray minnow
#

put a UnityEvent

#

in the script that sets the text

whole cove
#

ohhhh right

#

okay

#

what's the syntax for that?

#

i'm thinking something like
readInput.onEndEdit.AddListener(currentWord)

gray minnow
#

first do this

#

one step at time

#

save it and look at inspector

#

so you can see what going on

whole cove
#

oh okay

gray minnow
#

this goes in the main script

#

where you change text

whole cove
#

where in the inspector am i looking?

gray minnow
#

ReadInput

whole cove
#

oh okay okay

#

oh i see it lol

#

weird

#

current word changes fine in the inspector

#

but not in the debug

gray minnow
#

wait

#

one step at time

gray minnow
#

screenshot it

whole cove
#

on sec

gray minnow
# whole cove

you have to tell the other script the name is changed 🙂

#

ok you did not add UnityEvent to script though

#

Start method is printing the debug is it not?

whole cove
#

nope

#

yes

#

should it go in update?

gray minnow
#

yea that is something Unity calls

#

nah

#

make an event

#

its very easy

whole cove
#

but I'm getting currentWord in inspector... isn't that odd?

gray minnow
#

its showing the serialized field that is public

whole cove
#

ohhh

#

but it's not being updated by the script I want to update it to

gray minnow
#

exactly

#

you have to tell it somehow

whole cove
#

okay I added an event listener

#

public UnityEvent eventListener;

gray minnow
#

no call it something better

whole cove
#

wordListener lol

gray minnow
#

public UnityEvent currentWordChanged;

whole cove
#

ahh

#

yes that's better

gray minnow
#

also it has to pass a string

#

so with that we add <string>

#

public UnityEvent<string> currentWordChanged;

#

this is the event

#

it will pass string to whoevever gets the event

whole cove
#

right

gray minnow
#

now save it

whole cove
#

kk

gray minnow
#

and look at the inspector

whole cove
#

woahhhh coooolllllllll

#

there's my words

gray minnow
#

no..

#

it didn't show up

whole cove
#

no? lol

gray minnow
#

did you add the UnityEvent?

#

I dont see it in the inspector

whole cove
#

yeah i added it to my ChangeImage.cs script

gray minnow
#

no

#

that goes in Input script

#

i told you already before lol

whole cove
#

oh my bad lol

gray minnow
#

thats the one that has current word we want to pass with event

whole cove
gray minnow
#

Great!

#

now inside ChangeImage

#

make a method to get that value

whole cove
#

wait for some reason my word list isn't updating

gray minnow
#

wait dont worry about that

whole cove
#

okay

gray minnow
#

the changeimage script needs to still get the string

#

via event

#

make a method inside ChangeImage that gets a string

#

make sure its public

whole cove
#

readInput.AddEventListener();

gray minnow
#

wait no

whole cove
#

Someting like that?

gray minnow
#

dont do that

whole cove
#

lol okay

gray minnow
#

you need a method

#

do you know what a method is

#

its a way to do something

whole cove
#

umm something like private void doSomething(){}

gray minnow
#

right

#

but it has to be public and needs to recieve a type string

#

do you know how to pass stuff through method ?

whole cove
#

yup one sec

#

okay I created a method

#
    public void receiveString(string s)
    {

    }
gray minnow
#

perfect

#

now put Debug.Log(s); inside if you want

#

for test

#

the last step is to do the same thing you did for InputField

#

click + button on the Input script with UnityEvent

#

drag in this method inside

whole cove
#

okay let me try

gray minnow
#

it should be the one that says Dynamic

#

you should see this method in there

whole cove
#

shoot i'm lost as to what to type to get call the event

gray minnow
#

did you plug the method first

whole cove
#

wdym plug

gray minnow
#

on ReadInput.cs

#

inspector you see there is + button

whole cove
#

oh no

gray minnow
whole cove
#

There's no function

gray minnow
#

oh you did

whole cove
#

yeah

gray minnow
#

click the down arrow

whole cove
#

yeah nothing

gray minnow
#

find the script

#

make sure you dragged the Object that has the script ChangeImage

#

not the script from project folder

#

make sure its the one from gameobject

#

you should be able to find the method

whole cove
gray minnow
#

great

#

now just call it right

whole cove
#

wdym call it right?

gray minnow
#

an event is invoked with Invoke()

#

so it would be currentWordChanged?.Invoke()

#

? this symboll is just for null checking, usually not needed in unity event

#

and since its a string event

#

it expects a string

whole cove
gray minnow
#

nooo ofc we call this from the script that has the event lol

whole cove
#

lol

#

dang

gray minnow
#

put Debug.Log inside reciveString

#

Debug.Log(s);

whole cove
gray minnow
#

yes but every time it changes

#

and the error is telling you what I told you

#

it expects a string

#

and what are we passing around that we care about and did this whole thing ?

whole cove
#

we're passing around the words?

gray minnow
#

also currentWord has no Invoke

#

thats not how that works

#

Invoke is called from event

whole cove
#

wait slow down

#

let me think about what's happening

gray minnow
#

its like a notification

#

same concept

#

lol you literally just made it, which one is the event

whole cove
#

so i've created an event listener and now i'm trying to pass it?

gray minnow
#

no this isnt the listener

#

this is the Invoker

#

it doesnt care about other scripts

#

it just calls an event aka Invoke()

#

any scripts that "listens" gets notified

#

the only care about the event

#

this event passses a string

#

so other scripts listening can do whatever when they get this new string

#

the listener in this case is the ChangeImage script

#
currentWordChanged?.Invoke(currentWord); 
//Invoke event for everyone with value of string currentWord  ```
whole cove
#

okay I invoked it

gray minnow
#

how does receiveString look

whole cove
#
    public void receiveString(string s)
    {
        Debug.Log(s);
    }
gray minnow
#

ok good now delete the whole Start method

whole cove
#

done

gray minnow
#

ok did you also put the invoke the other spot where you change word

#

cause only awake will just invoke once

whole cove
#

GetRandomWord?

gray minnow
#

or it runs twice

whole cove
gray minnow
#

no...

#

you deleted the wrong start

#

lol

whole cove
#

yeah I deleted change image lol

#

is it necessary to delete?

gray minnow
#

oh u just moved the code to awake here

#

yeah you dont need awake or start inside ChangeImage

whole cove
#

yeah

gray minnow
#

anyway move currentWordChanged?.Invoke(currentWord); from awake

#

to maybe before return of change word?

#
 //  Remove the selected word from the list
        placeholderWords.RemoveAt(randomIndex);
        //alert other scripts word changed
        currentWordChanged?.Invoke(currentWord);
        // Return the selected word
        return randomWord;```
#

if you followed everything , you should now see console print every time you change word

whole cove
#

Where should I put those two lines?

gray minnow
#

look carefully

#

its your own code xD

#

try to somewhat get familiar with it

whole cove
#

okay lol

#

oh d'oh

#

i wasn't paying attention hahahahaha

#

awesome it works

#

only issue is that it prints the debug a lot haha

gray minnow
#

hmm send both current scripts real quick

#

want to check something

gray minnow
#

there is ur problem

#
  void Update()
    {
        Debug.Log(readInput.currentWord);
    }
whole cove
#

also this is odd but it wont update the text anymore, it is stuck on japanese

gray minnow
#

// Update is called once per frame

whole cove
#

oh hahahahahahaha

gray minnow
#

now you're getting the value thru event

#

everytime its Invoked, which is GetRandomWord

whole cove
#

should i delete serializedfield?

gray minnow
whole cove
#

kk

#

and what do we do about the word not changing?

gray minnow
#

those values wont change now

whole cove
#

Placeholder words

gray minnow
#

you have to use the inspector

#

it already serialized the first time with those words

#

so any changes to script init fields wont change

whole cove
#

so is placeHolder words even necessary anymore?

gray minnow
#

string randomWord = placeholderWords[randomIndex];

#

you are using it

#

if you want to change values now, you use the + - in the inspector list

whole cove
#

i really need to go through this code and understand what's happening

#

thank you so much for helping me

gray minnow
#

sure no problem!
its not easy jumping into these intricate mecahnics without knowing some of the basics of code 🙂

whole cove
#

yup ypu :)