How to make a Field Declaration for the Script that is in the Instantiated Object?

GameObject newNPC = Instantiate(npcPrefab, spawnPosition, Quaternion.identity);
HumanNPCAttributes npcAttributes = newNPC.GetComponent<HumanNPCAttributes>();
//newNPC.AddComponent<HumanNPCAttributes>();
npcAttributes.SetAttributes(NPCName , age, maxmood, maxHunger);

I have a npcPrefab and I want to create them in the game so I used Instantiate() and it’s working well but in my Instantiated npcPrefabs I have HumanNPCAttributes script and for this script to work I need to set public NPCCreator NPCCreator;.

I tried to use

GameObject npcPrefab = GameObject.Find("NPCCreator");

but couldn’t get it working so I don’t know if it’s the right answer for my problem.

What should I do?

Are you trying to create a new field/attribute in the HumanNPCAttributes class at runtime? Or, does that field already exist in the class. If it doesn’t exist, why not just hard code the field as part of the class?


like in here I created my npcPrefab and added scripts that are needed but in HumanNPCAttributes how can I set the NPCCreator automatically?
My NPCCreator script is in the empty game object named NPCCreator.

NPCCreator = GameObject.FindGameObjectWithTag("NPCCreator").GetComponent<NPCCreator>();