r/UnityHelp • • Sep 10 '25
PROGRAMMING How can I read variables from spawned prefabs?
I have an object that generates a random letter upon spawning, and I have another object that spawns 3 of these tiles in. I want to be able to restrict the player's input to whatever letters got generated, but I'm not sure how to get back whatever letter has been generated. I would like my script to be able to read the "generatedLetter" variable of each of the tiles
5
Upvotes


1
u/L4DesuFlaShG Sep 10 '25
Instantiatereturns the instantiated object.Awakeruns beforeInstantiatefinishes.So you can do this:
with
1
u/GameGirlAdvanceSP Sep 10 '25
I would reference the object in the prefab script and then add a function that sends prefab's variable value to it. It may be necessary to create an array the size of the number of prefab's objects in order to store multiple values.
1
u/sysknot Sep 10 '25
I think there's a lot of ways to implement it, i usually mantain separated the logic of things, and mantain the communications by events. In this case i would use a, for example Action<char> OnLetterGenerated, and when you instatiate the prefab, you also call the event: OnLetterGenerated?.Invoke(generatedLetter)
0
u/One-Membership9101 Sep 10 '25
Create method in class initializeLetters
public string GetGeneratedLetter ()
{
return generatedLetter;
}
Store links for each spavned object as List of initializeLetters.
Then just call GetGeneratedLetter
0
u/One-Membership9101 Sep 10 '25
If you need more help - ping me in DM