#Prefabs and serialized fields issue

1 messages · Page 1 of 1 (latest)

winged rock
#

Hello! I'm writing a script that will take a generic playing card Game Object prefab and clone it, applying different values pulled from a database to every new clone. To accomplish that, I'm trying to add these TextMeshPro UGUI elements from a prefab:

#

to these serialized fields in a script component:

#

This works if I do it from a clone in the scene, but if I try to do this from the prefab itself, Unity simply won't let me connect them. And doing it from a clone in the scene doesn't work because the assignment of the values from the databse is thrown off. Does anyone know why this might be happening?

silver silo
#

sounds like you're trying to drag maybe some objects from the scene into the prefab or something?

winged rock
#

Sorry -- pretty new to unity/C#. Thanks for the reply. I'm trying to drag four TextMesh game objects into the four fields on the Hand Builder script. I'm trying to drag those for game objects from a prefab, to the script, which is on a non-prefab game object.

winged rock
#

Ahh, okay.

silver silo
#

DO you have a script on the CardGeneric prefab object?

#

You should put a script on that, and put those 4 fields on that script

winged rock
#

No, the script is on an object called HandBuilder.

#

I see

silver silo
#

and give that script a method like:

public void PopulateCardText(CardInfo cardInfo) {
  myCardTitleText.text = cardInfo.title;
  myAttribute1Text.text = cardInfo.attribute1;
  // etc...
}```
winged rock
#

Got it. So it's okay to put the script on the prefab of the thing I plan to be cloning?

silver silo
#

then HandBuilder can do this:

Card cardInstance = Instantiate(cardPrefab);
card.PopulateCardText(someCardInfo);
#

You should 100% have a script on the prefab

#

It's kind of insane not to

#

You want each object to have as little knowledge about other objects as possible

#

it makes things easier to maintain

winged rock
#

Okay great. I actually put all the attribute text stuff into the handbuilder script too. It's all in there with the cloning code. But I'll split up the scripts.

#

Thanks so much! I'll dive in and give that a shot.