Instead of giving spawnable prefabs scripts sometimes I just name them with information data but I forgot how I used to do that.
It was something like this.
Spawner:
public class SpawnIt : MonoBehaviour
{
ReadIt readS;
public GameObject prefabObj;
GameObject spawnClone;
public void Start(){
readS = GameObject.Find("ReadObj")GetComponent<ReadIt>();
spawnClone = Instantiate(prefabObj);
spawnClone.name = "(name,hats),(id, readS.numberOfItemsSpawnedInTotal),(other,true)";
}
}
Reader:
public class ReadIt : MonoBehaviour
{
//ItemData
[Header("String Data")]
GameObject findCloneObj;
string[] itemGroupName; // names Hats, Cats...
int[] groupIdHats;
int[] groupIdCats;
int[] groupid...;
bool[] groupHatsState;
bool[] groupCatsState;
bool[] group...;
public void ReadAndStoreInfo(){
findCloneObj = GameObject.Find("hats, 234, true")
//or if need to get infomation that already egsists
findCloneObj = GameObject.Find(itemGroupName + groupIdHats + groupHatsState")
//take information and crate and asighn it to there respective group varable
}
}
If you insist on implementing a string-embedded naming scheme, become 100% solidly familiar with how to do these operations:
take the different quantities and reliably assemble the identifying string (eg., 123 and apple should ALWAYS produce exactly “123,apple” and NEVER anything else, even one character different will fail to work)
pass that to GameObject.Find() (see note above about not using GameObject.Find()… you’re kind of on your own as it is well-identified as a problematic API, even by Unity themselves in their own documentation)
proceed to do what you want with what you find, such as calling .GetComponent<T>() on it
understand that this approach may have serious scalability, fragility and stability issues as your game continues development.
You can also circumvent a lot of these issues by tracking the items yourself, perhaps even in a Dictionary<string,GameObject>() that you fill out and update over time. That will be nearly instantaneous to search versus however long GameObject.Find() might take.