Pages - Menu

2014年2月4日火曜日

[Unity][C#]GameObjectにアタッチされたコンポーネントを参照する方法

Scoreオブジェクトにアタッチされたスクリプトを取得して、変数を処理
using UnityEngine;
using System.Collections;

public class Score : MonoBehaviour {
 public int score;
 // Use this for initialization
 void Start () {
  score = 30; 
 }
 
 // Update is called once per frame
 void Update () {
  guiText.text = score.ToString ();
 }
}


using UnityEngine;
using System.Collections;

public class Spawner : MonoBehaviour {
 public Transform prefab;
 // Use this for initialization
 void Start () {
 }
 
 // Update is called once per frame
 void Update () {
  GameObject score = GameObject.Find("Score");
  Score scoreScript = score.GetComponent();
  scoreScript.score -= 1;
 }
}

0 件のコメント:

コメントを投稿