Hello everyone i was trying to do a simple avatar assignation by using a dynamic prefab starter in the network manager. unfortunately i have some problems on accessing the value of a gender button that changes its text value everytime someone presses it. The button is in the scene but the dynamic prefab starter is not. How can I do it?
public class characterSpawner : NetworkBehaviour
{
[SerializeField] private NetworkObject prefabMale;
[SerializeField] private NetworkObject prefabFemale;
public override void OnNetworkSpawn()
{
//Button yourButton = yourObject.GetComponent<Button>();
string buttonText = "Female";
Button btn = (Button)FindObjectOfType(typeof(Button));
if (!IsServer) { return; }
if(buttonText == "Male")
{
var spawnPos = new Vector3(Random.Range(-3f, 3f), 0f, Random.Range(-3f, 3f));
var characterInstance = Instantiate(prefabMale, spawnPos, Quaternion.identity);
}
else
{
var spawnPos = new Vector3(Random.Range(-3f, 3f), 0f, Random.Range(-3f, 3f));
var characterInstance = Instantiate(prefabFemale, spawnPos, Quaternion.identity);
}
}
}
I tried some stuff such as accessing via a [serializedvar] the button but since this script is associated to the prefab in the networkmanager and is not in the scene it can’t see the button. I tried to do a “GetBtnValue” method in another script associated to a ServerManager in the scene but also for some reasons don’t work.
fedesbuc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.