質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

1回答

1462閲覧

UnityのPhotonでRayが触れたオブジェクトのSliderを変更したい。

Kuro515

総合スコア25

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/05/16 06:35

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は減りません。どうしたらよいでしょうか?

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

HP.a プロパティの値を操作したら Slider が動くようにしたいんですよね?

csharp

1 void Update() 2 { 3 a = GetComponent<Slider>().value; 4 }

逆じゃないですか?つまり GetComponent<Slider>().value = a; じゃないですか?

投稿2020/07/20 21:37

bboydaisuke

総合スコア5275

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問