#make a thread I'll explain it
1 messages · Page 1 of 1 (latest)
Ok so basically when you do say public or [SerializeField] private UnityEvent myEvent
now you see in the inspector
right
there is the same thing as the InputField?
wdym
hold on my inspector is frozen
alr
okay back
you did it?
no no
put a UnityEvent
in the script that sets the text
ohhhh right
okay
what's the syntax for that?
i'm thinking something like
readInput.onEndEdit.AddListener(currentWord)
for example public UnityEvent whatever
first do this
one step at time
save it and look at inspector
so you can see what going on
oh okay
where in the inspector am i looking?
on the script you just added the line to
ReadInput
oh okay okay
oh i see it lol
weird
current word changes fine in the inspector
but not in the debug
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?
but I'm getting currentWord in inspector... isn't that odd?
why would it be odd ?
its showing the serialized field that is public
no call it something better
wordListener lol
public UnityEvent currentWordChanged;
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
right
now save it
kk
and look at the inspector
no? lol
yeah i added it to my ChangeImage.cs script
oh my bad lol
thats the one that has current word we want to pass with event
wait for some reason my word list isn't updating
wait dont worry about that
okay
the changeimage script needs to still get the string
via event
make a method inside ChangeImage that gets a string
make sure its public
readInput.AddEventListener();
wait no
Someting like that?
dont do that
lol okay
umm something like private void doSomething(){}
right
but it has to be public and needs to recieve a type string
do you know how to pass stuff through method ?
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
okay let me try
shoot i'm lost as to what to type to get call the event
thats the last step
did you plug the method first
wdym plug
did you click + sign on unity event CurrentWordChanged
on ReadInput.cs
inspector you see there is + button
oh no
yeah
click the down arrow
yeah nothing
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
wdym call it right?
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
nooo ofc we call this from the script that has the event lol
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 ?
we're passing around the words?
also currentWord has no Invoke
thats not how that works
Invoke is called from event
its like a notification
same concept
lol you literally just made it, which one is the event
so i've created an event listener and now i'm trying to pass it?
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 ```
public void receiveString(string s)
{
Debug.Log(s);
}
ok good now delete the whole Start method
done
ok did you also put the invoke the other spot where you change word
cause only awake will just invoke once
GetRandomWord?
if you put it there you have to delete invoke from the awake method
or it runs twice
oh u just moved the code to awake here
yeah you dont need awake or start inside ChangeImage
yeah
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
Where should I put those two lines?
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
yeah Over 9999+ times
hmm send both current scripts real quick
want to check something
also this is odd but it wont update the text anymore, it is stuck on japanese
// Update is called once per frame
oh hahahahahahaha
in fact you dont really need SerializeField] private ReadInput readInput; anymore for this
now you're getting the value thru event
everytime its Invoked, which is GetRandomWord
should i delete serializedfield?
you can
where is not changing ?
those values wont change now
you have to use the inspector
it already serialized the first time with those words
so any changes to script init fields wont change
so is placeHolder words even necessary anymore?
string randomWord = placeholderWords[randomIndex];
you are using it
if you want to change values now, you use the + - in the inspector list
i really need to go through this code and understand what's happening
thank you so much for helping me
sure no problem!
its not easy jumping into these intricate mecahnics without knowing some of the basics of code 🙂
yup ypu :)