Global Script

Options

Hello everyone, I have a script on an object, this script when a player clicks "G" disappears some texts (GameObjects) but I wanted to globally for all players in the room, any player who clicks the first key will disable these objects for all players.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class StartGameAutorized : MonoBehaviour {

	public GameObject UI;
	public GameObject FPSCam;
	public GameObject Spawnermanager;
	private Text waitAutorized;
	private bool gameStarted = false;

	// Use this for initialization
	void Start () {


	}
	
	// Update is called once per frame
	void Update () {

		Spawnermanager = GameObject.Find ("Spawner");
		FPSCam = GameObject.Find ("FPSCamera");
		UI = GameObject.Find ("UI");
		waitAutorized = GameObject.FindWithTag ("waitAutorized").GetComponent<Text> ();

		if (Input.GetKeyDown ("g")) {

			StartGame ();

		}

	}


	public void StartGame(){

		//HELP-ME SCRIPT HERE DISABLE COMPONENTS FOR EVERYONE:

		FPSCam.GetComponent<MoneyBuyAndOpens> ().enabled = true;
		Spawnermanager.GetComponent<SpawnerA> ().enabled = true;
		UI.transform.FindChild("Canvas").gameObject.SetActive(true);
		waitAutorized.enabled = false;
		gameStarted = true;



	}
}


Comments

  • Frosty
    Options
    Hey,

    In order to call StartGame() on everyone else, you need to send an RPC. To do that, firstly add a "PhotonView" component to the object that uses your script "StartGameAutorized". Then, this is what your code should look like.
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class StartGameAutorized : Photon.MonoBehaviour {
    
    	public GameObject UI;
    	public GameObject FPSCam;
    	public GameObject Spawnermanager;
    	private Text waitAutorized;
    	private bool gameStarted = false;
    
    	// Use this for initialization
    	void Start () {
                Spawnermanager = GameObject.Find ("Spawner");
    		FPSCam = GameObject.Find ("FPSCamera");
    		UI = GameObject.Find ("UI");
    		waitAutorized = GameObject.FindWithTag ("waitAutorized").GetComponent<Text> ();
    	}
    	
    	// Update is called once per frame
    	void Update () {
    		if (Input.GetKeyDown ("g")) {
    			photonView.RPC("StartGame", PhotonTargets.All);
    		}
    	}
    
            [PunRPC]
    	public void StartGame(){
                    if (gameStarted) { return; }
    
                    gameStarted = true;
    		FPSCam.GetComponent<MoneyBuyAndOpens> ().enabled = true;
    		Spawnermanager.GetComponent<SpawnerA> ().enabled = true;
    		UI.transform.FindChild("Canvas").gameObject.SetActive(true);
    		waitAutorized.enabled = false;
    	}
    }
    


    By the way, do not do "GameObject.Find" in the Update() function because this is very slow performance wise. Do these in the start unless you really need to find them in each update.
  • TyHover
    Options


    What Config?
  • Frosty
    Frosty
    edited April 2017
    Options
    You don't have to change them.

    However, I can already see that your PhotonView is not attached to the same object that has the StartGameAutorized component.

    If you want to keep them separate, like you've done currently, then in the code change
    photonView.RPC("StartGame", PhotonTargets.All);

    to

    waitAutorized.GetComponent<PhotonView>().RPC("StartGame", PhotonTargets.All);
  • TyHover
    TyHover
    edited April 2017
    Options
    PhotonView with ID 165 has no method "StartGame" marked with the [PunRPC](C#) or @PunRPC(JS) property! Args:
    UnityEngine.Debug:LogError(Object

    I tried changing the script ID (PhotonView) but it did not work anyway
  • TyHover
    TyHover
    edited April 2017
    Options
    I put the script in my object with component Text, and I got it, Thanks Frosty for help-me

    final script,To help other people
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class StartGameAutorized : MonoBehaviour {
    
    	private GameObject UI;
    	private GameObject FPSCam;
    	private GameObject Spawnermanager;
    	private Text waitAutorized;
    	private bool gameStarted = false;
    
    	// Use this for initialization
    	void Start () {
    
    
    	}
    	
    	// Update is called once per frame
    	void Update () {
    
    		if (Input.GetKeyDown ("g")) {
    			
    			gameObject.GetComponent<PhotonView>().RPC("StartGame", PhotonTargets.All);
    
    		}
    
    	}
    
    	[PunRPC]
    	public void StartGame(){
    
    		//DISABLE COMPONENTS FOR EVERYONE:
    
    		waitAutorized = gameObject.GetComponent<Text> ();
    		Spawnermanager = GameObject.Find ("Spawner");
    		FPSCam = GameObject.Find ("FPSCamera");
    		UI = GameObject.Find ("UI");
    		FPSCam.GetComponent<MoneyBuyAndOpens> ().enabled = true;
    		Spawnermanager.GetComponent<SpawnerA> ().enabled = true;
    		UI.transform.FindChild("Canvas").gameObject.SetActive(true);
    		waitAutorized.enabled = false;
    		gameStarted = true;
    
    
    
    	}
    }