- Home /
Control spawned Photon Player with spawned new UI
In my game scene there are only 2 players. First player who creates the room will spawn by pressing "Spawn" button. And "Spawn" button disactivated for him and spawns new UI control HUD buttons. Which allows to player move, rotate, shoot etc. It works good if there is only one player in room. I already set up "OnClick" Events. When UI Instantiated, it founds gameobjects with tag.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Firing : MonoBehaviour {
public GameObject myPlayer;
void Start ()
{
myPlayer = GameObject.FindWithTag ("Duo");
}
public void FireShoot ()
{
myPlayer.GetComponent<Fire> ().Shoot ();
}
}
// Another script
using UnityEngine;
using System.Collections;
public class ConnectHUD : Photon.MonoBehaviour {
void Start ()
{
if (photonView.isMine)
{
gameObject.GetComponent<Firing>().enabled = true;
}
}
}
But pain occurs after second players spawns and when I press "Fire" button for second player. UI HUD does not finds GameObjectWithTag for second player. It gives me an error.
// ERROR MESSAGE UnassignedReferenceException: The variable myPlayer of Firing has not been assigned. You probably need to assign the myPlayer variable of the Firing script in the inspector.
if I assign "myPlayer" to HUD manually in Inspector for the second player when the game runs, it works fine. But how can I work it properly. There are 2 players and 1 HUD control buttons. Is problem GameObjec.tFindWithTag? Because there are 2 players in the scene with same Tag. Is there must be something like FindGameObject if it is LocalPlayer?
I found solution. It is because of "tag". there are 2 player, both of them have same object and their same objects have same tag. So I have changed Tag in script. Now its works.
public GameObject myPlayer;
void Start ()
{
if (PhotonNetwork.is$$anonymous$$asterClient)
{
myPlayer = GameObject.FindWithTag ("DuoLeft");
}
if (!PhotonNetwork.is$$anonymous$$asterClient)
{
myPlayer = GameObject.FindWithTag ("DuoRight");
}
}
public void FireShoot ()
{
myPlayer.GetComponent<Fire> ().Shoot ();
}
//////////////////////////////////
void Start ()
{
if (photonView.is$$anonymous$$ine)
{
//GetComponent<Change$$anonymous$$aster> ().enabled = true;
//GetComponent<BoxCollider> ().enabled = true;
//GetComponent<Animator>().enabled = true;
//FireDulo.GetComponent<Fire>().enabled = true;
//myCannon = GameObject.FindWithTag ("Player");
//anim = GetComponent<Animator>();
Instantiate(canvas);
FireDulo.GetComponent<Fire>().enabled = true;
//UIbutton.SetActive (true);
if (PhotonNetwork.is$$anonymous$$asterClient)
{
FireDulo.tag = "DuoLeft";
}
if (!PhotonNetwork.is$$anonymous$$asterClient)
{
FireDulo.tag = "DuoRight";
}
}
else
{
StartCoroutine("UpdateData");
}
//Instantiate(canvas);
anim = GetComponent<Animator>();
myCannon = GameObject.FindWithTag ("Player");
GetComponent<Change$$anonymous$$aster> ().enabled = true;
GetComponent<BoxCollider> ().enabled = true;
GetComponent<Animator>().enabled = true;
//FireDulo.GetComponent<Fire>().enabled = true;
//UI = GameObject.FindWithTag ("HUD");
//UI.GetComponent<Firing> ().enabled = true;
gameObject.name = gameObject.name + photonView.viewID;
}
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Controlling pause menu with keyboard in Unity 4.6? 1 Answer
Photon Instantiate Prefab Script Problems 0 Answers
Access a clone of an instantiated object - not all the clones? 2 Answers
PhotonNetwork.InstantiateSceneObject works in editor but not in build 2 Answers