Unity3d : 게임 오브젝트에 접근하기(Accessing Game Objects)
C#에서
게임 오브젝트에 접근하는 방법들
transform.Find("오브젝트 이름"); // 자식이나 부모 관계 안에서만 찾음
GameObject.Find("오브젝트 이름"); // Hierarchy안에서 다 찾음
GameObject.FindWithTag("태그 이름"); // 해당하는 태그에 속하는 오브젝트 반환.
여러 개일 경우 그 중에 랜덤으로 반환
컴포넌트에 접근하는 방법
해당 오브젝트.GetComponent<컴포넌트 이름>();
~~ example.cs
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Transform target;
public Otherscript target2;
void OnTriggerStay(Collider other) {
if (other.rigidbody)
other.rigidbody.AddForce(0, -2, 0);
if (other.GetComponent<Otherscript>())
other.GetComponent<Otherscript>().DoSomething("Hello");
}
// Use this for initialization
void Awake() {
transform.Find("Hand").Translate(0, 1, 0);
transform.Find("Hand").GetComponent<Otherscript>().foo=2;
transform.Find("Hand").GetComponent<Otherscript>().DoSomething("Hello");
transform.Find("Hand").rigidbody.AddForce(0, 10, 0);
foreach (Transform child in transform) {
child.Translate(0, 1, 0);
}
}
void Start () {
GameObject go = GameObject.Find("SomeGuy");
go.transform.Translate(0, 1, 0);
go.GetComponent<Otherscript>().DoSomething("Hello");
GameObject player = GameObject.FindWithTag("Player");
go.GetComponent<Otherscript>().DoSomething("Hello");
player.transform.Translate(0, 1, 0);
}
// Update is called once per frame
void Update () {
target.Translate(0,0.1f,0);
target2.foo = 2;
target2.DoSomething("Hello");
}
}
~~ Otherscript.cs
using UnityEngine;
using System.Collections;
public class Otherscript : MonoBehaviour {
public int foo = 0;
public void DoSomething(string str) {
}
}
게임 오브젝트에 접근하는 방법들
transform.Find("오브젝트 이름"); // 자식이나 부모 관계 안에서만 찾음
GameObject.Find("오브젝트 이름"); // Hierarchy안에서 다 찾음
GameObject.FindWithTag("태그 이름"); // 해당하는 태그에 속하는 오브젝트 반환.
여러 개일 경우 그 중에 랜덤으로 반환
컴포넌트에 접근하는 방법
해당 오브젝트.GetComponent<컴포넌트 이름>();
~~ example.cs
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Transform target;
public Otherscript target2;
void OnTriggerStay(Collider other) {
if (other.rigidbody)
other.rigidbody.AddForce(0, -2, 0);
if (other.GetComponent<Otherscript>())
other.GetComponent<Otherscript>().DoSomething("Hello");
}
// Use this for initialization
void Awake() {
transform.Find("Hand").Translate(0, 1, 0);
transform.Find("Hand").GetComponent<Otherscript>().foo=2;
transform.Find("Hand").GetComponent<Otherscript>().DoSomething("Hello");
transform.Find("Hand").rigidbody.AddForce(0, 10, 0);
foreach (Transform child in transform) {
child.Translate(0, 1, 0);
}
}
void Start () {
GameObject go = GameObject.Find("SomeGuy");
go.transform.Translate(0, 1, 0);
go.GetComponent<Otherscript>().DoSomething("Hello");
GameObject player = GameObject.FindWithTag("Player");
go.GetComponent<Otherscript>().DoSomething("Hello");
player.transform.Translate(0, 1, 0);
}
// Update is called once per frame
void Update () {
target.Translate(0,0.1f,0);
target2.foo = 2;
target2.DoSomething("Hello");
}
}
~~ Otherscript.cs
using UnityEngine;
using System.Collections;
public class Otherscript : MonoBehaviour {
public int foo = 0;
public void DoSomething(string str) {
}
}
'유니티3D > 자유게시판' 카테고리의 다른 글
Unity 3D JsonFX 사용 (0) | 2016.08.31 |
---|---|
유니티 unity 모바일 터치 (0) | 2016.08.31 |
unity 3D 오브젝트 무료사이트 추천! (0) | 2016.08.01 |
unity 구글플레이 게임서비스 (Google Play Game Service) 연동 (0) | 2016.08.01 |