【Unity】反応したオブジェクトの数を数える方法
今現在Unityを使用してFOVE向けのアプリケーションを作成しています。
その中で特定のオブジェクトがONになればカウント数を1増やすというプログラムが作りたいです。
今回はSetScoreを使ってテキスト出力したいです。
ある一定時間以上オブジェクトを見るとLightがTrueになるといったプログラムです。
Trueとなったオブジェクトの数を調べたいです。
このスクリプトは10個のオブジェクトに取り付けるものです。
以下のコードで試しましたが、カウント数は増えませんでした。
原因を教えてください。
よろしくお願いします。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class FOVELookSample : MonoBehaviour 7{ 8 public Light attachedLight; 9 public FoveInterfaceBase foveInterface; 10 11 private Collider my_collider; 12 private Material material; 13 private bool light_attached = false; 14 // Update is called once per frame 15 16 public float GazingTime;//public float GazingTime = 1.0f; 見つめ時間...何秒見つめ続けると反応するかを設定する 17 private float currentGazingTime; // 見つめ時間カウント用のフィールド 18 private bool lightEnabled; // 現在ライトが点灯中かどうかを示すフィールド 19 // Use this for initialization 20 public Text scoreText; //Text用変数 21 private int score = 0; //スコア計算用変数 22 23 24 void SetScore() 25 { 26 scoreText.text = string.Format("{0}/15", score); 27 } 28 29 void Start() 30 { 31 32 my_collider = GetComponent<Collider>(); 33 34 35 if (attachedLight) 36 { 37 light_attached = true; 38 attachedLight.enabled = false; 39 } 40 material = gameObject.GetComponent<Renderer>().material; 41 42 if (material == null) 43 gameObject.SetActive(false); 44 } 45 46 47 void Update() 48 { 49 GazingTime = dropdown.GetTime(); 50 if (lightEnabled) 51 { 52 } 53 else 54 { 55 // もしライト消灯中の状態で... 56 if (foveInterface.Gazecast(my_collider)) 57 { 58 // Gazecastがヒットした場合... 59 60 // currentGazingTimeを経過時間分だけ減らす 61 currentGazingTime -= Time.deltaTime; 62 63 if (currentGazingTime <= 0.0f) 64 { 65 // currentGazingTimeが0以下になったら、見つめ続けてGazingTime秒以上経過したので、ライトを点灯する 66 lightEnabled = true; 67 score += 1; 68 material.EnableKeyword("_EMISSION"); 69 70 71 if (light_attached) 72 { 73 material.SetColor("_EmissionColor", attachedLight.color); 74 attachedLight.enabled = false; //ライトが点く 75 DynamicGI.SetEmissive(GetComponent<Renderer>(), attachedLight.color); 76 } 77 } 78 } 79 else 80 { 81 // Gazecastがヒットしなかった場合、視線が外れたのでcurrentGazingTimeをGazingTimeにリセット 82 currentGazingTime = GazingTime; 83 } 84 } 85 86 if (score >= 0) 87 { 88 SetScore(); 89 } 90 } 91} 92
回答1件
あなたの回答
tips
プレビュー