I tried to ‘Start’ creation of the image, and after reference
But it seems no effect, and did not get a reference
I can only get objects reference ‘Update’ in
What should I do to after the ‘Start’ or you can get a reference?
[SyncVar]public int MyID;
public GameObject P_body;
public GameObject P_ind;
public GameObject Mybody;
public GameObject Myind;
void Start () {
transform.name = "PID"+GetComponent<NetworkIdentity>().netId; //Give the player ID and rename
MyID=int.Parse( GetComponent<NetworkIdentity>().netId.ToString() ) ;//Set Player ID
}
public override void OnStartLocalPlayer () {
callobj();
}
public void FindMy(){
Debug.Log (MyID+"BODY");
Mybody = GameObject.Find(MyID+"BODY");
Myind = GameObject.Find(MyID+"IND");
}
[Client]
public void callobj(){
Cmdcobj(transform.position);
}
[Command]
public void Cmdcobj(Vector3 lp){
lp+= new Vector3(0,20,0);
GameObject nbody = (GameObject)Instantiate(P_body,lp,transform.rotation);
nbody.GetComponent<BID>().myid = MyID;
NetworkServer.Spawn(nbody);
GameObject nind = (GameObject)Instantiate(P_ind,lp,transform.rotation);
nind.GetComponent<BID>().myid = MyID;
NetworkServer.Spawn(nind);
}
I had a similiar problem. When the start method runs the id has not been set yet - its running before all the network bits have been set up. I’m grabbing the ID in an Update. You can always turn the script off when you’re done.
fafase
3
Create a event/listener system.
private NetworkIdentity networkIdentity = null;
private void Awake()
{
networkIdentity = GetComponent<NetworkIdentity>();
if(networkIdentity.IsIDReady == false){
networkIdentity.OnIDReady += HandleOnIDReady;
}
else{
// Grab it as usual
}
}
private void OnDestroy(){
if(networkIdentity != null){
networkIdentity.OnIDReady -= HandleOnIDReady;
networkIdentity = null;
}
}
private void HandleOnIDReady(string id){
// Grab id as usual
// And do what you got to do with it
}
Then the NetworkIdentity should implement the event:
public Action<string>OnIDReady = (str) => { };
private void MakeID(){
string id = null;
// Here the ID is made, Dunno how
// ID is done
OnIDReady(id);
}