GameObject类:

在脚本中获取游戏对象

常用的方法属性

代码实例:
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
void Start () {
Debug.Log(gameObject.name);
gameObject.name = "老王";
Debug.Log(gameObject.tag);
gameObject.tag = "Player";
Debug.Log(gameObject.activeSelf);
gameObject.SetActive(false);
CubeController c = gameObject.GetComponent<CubeController>();
Debug.Log(c.balabala);
Light l = gameObject.AddComponent<Light>();
GameObject g = GameObject.FindGameObjectWithTag("Player");
g.name = "老王";
GameObject gg = GameObject.FindWithTag("Player");
GameObject g1 = GameObject.Find("Main Camera");
g1.name = "主摄像机";
GameObject[] gs = GameObject.FindGameObjectsWithTag("Player");
}
void Update () {
if (Input.GetKeyDown(KeyCode.X))
{
GameObject.Destroy (gameObject,2f);
}
}
}
CubeController(5)组件
using UnityEngine;
using System.Collections;
public class CubeController : MonoBehaviour {
public string balabala;
}