Unityでオンラインゲームを作っています。
クリックで、Rayを発射し、相手に触れたら、そのオブジェクトの子供のSliderのValueを減らしたいと思っています。
しかし、
C#
1using System; 2using System.Collections; 3using UnityEngine; 4using UnityEngine.UI; 5using Photon.Pun; 6 7 8public class Hassya : MonoBehaviour 9{ 10 [SerializeField] private string Player = "Player"; 11 [SerializeField] private Material highlightMaterial; 12 [SerializeField] private Material defaultMaterial; 13 14 private Transform _selection; 15 public AudioClip sound1; 16 public AudioClip re; 17 public AudioSource audiosource; 18 public GameObject cam; 19 public Camera came; 20 public float zandan; 21 String z; 22 public Text zandanText; 23 public Text zandanTextPC; 24 public Image Dot; 25 public Image Aim; 26 bool a; 27 public GameObject SettingCanvas; 28 float TimeCount; 29 30 private int timeCount; 31 32 public GameObject h; 33 public GameObject b; 34 35 float killcount; 36 public Text killPC; 37 public Text kill; 38 public float tekihp; 39 40 PhotonView m_photonView; 41 void Start() 42 { 43 zandan = 30; 44 zandanText.text = "30"; 45 zandanTextPC.text = "30"; 46 } 47 private void Update() 48 { 49 if (Input.GetMouseButtonDown(0)) 50 { 51 Cursor.lockState = CursorLockMode.Locked; 52 hassya(); 53 } 54 timeCount += 1; 55 56 if (Input.GetButton("Click")) 57 { 58 59 if (timeCount % 8 == 0) 60 { 61 hassya(); 62 } 63 } 64 65 66 } 67 public void hassya() 68 { 69 70 syori(); 71 72 } 73 void syori() 74 { 75 76 if (zandan > 0) 77 { 78 zandan -= 1; 79 z = zandan.ToString(); 80 zandanTextPC.text = z; 81 audiosource.PlayOneShot(sound1); 82 83 RaycastHit hit; 84 if (Physics.Raycast(ray, out hit)) 85 { 86 87 var selection = hit.transform; 88 89 if (selection.tag == "Player") 90 { 91 92 h = selection.GetComponent<Jump>().Head; 93 b = selection.GetComponent<Jump>().Body; 94 var selectionRenderer = h.GetComponent<Renderer>(); 95 var selectionRenderer2 = b.GetComponent<Renderer>(); 96 97 if (selectionRenderer != null) 98 { 99 100 selectionRenderer.material = highlightMaterial; 101 102 float a = selection.GetComponent<Jump>().SliderPC.GetComponent<HP>().a; 103 a -= 3; 104 //↑で、触れたオブジェクトの中にあるSliderの変数を取得して、代入し、減らしている。 105 106 tekihp = a; 107 108 if (tekihp <= 0) 109 { 110 killcount += 1; 111 kill.text = killcount.ToString() + "人抜き"; 112 } 113 } 114 115 116 _selection = selection; 117 } 118 119 } 120 121 122 } 123 } 124 125 126 127 public void Call() 128 { 129 var selectionRenderer = h.GetComponent<Renderer>(); 130 var selectionRenderer2 = b.GetComponent<Renderer>(); 131 selectionRenderer.material = defaultMaterial; 132 selectionRenderer2.material = defaultMaterial; 133 _selection = null; 134 135 } 136 137
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using Photon.Pun; 5using UnityEngine.UI; 6 7public class HP : MonoBehaviour 8{ 9 public float a; 10 // Start is called before the first frame update 11 void Start() 12 { 13 14 } 15 16 // Update is called once per frame 17 void Update() 18 { 19 a = GetComponent<Slider>().value; 20 } 21 22 void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) 23 { 24 if (stream.IsWriting) 25 { 26 //データの送信 27 stream.SendNext(a); 28 } 29 else 30 { 31 //データの受信 32 a = (float)stream.ReceiveNext(); 33 } 34 } 35 36} 37
しかし、Sliderは減りません。どうしたらよいでしょうか?
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。