蓝鸥Unity开发教程之课时5 Unity组件开发之脚本组件
Test脚本
using UnityEngine;
using System.Collections;
//如果我们的脚步需要挂载到游戏对象身上,就需要继承于MonoBehaviour
public class Test : MonoBehaviour {
public int age;
public string name;
public void Log(){
print(age+name);
}
void Start () {
//gameObject表示当前脚本组件所挂载的游戏对象
//unity中输出到控制台使用Print或者Debuq.log
// print("Test 脚本挂载到了"+gameObject.name+"的身上");
//每个游戏对象身上,都至少有一个组件,叫做Transform
//transform表示当前游戏对象身上的,Transform组件
// print(transform.position.x);
// print(age+name);
}
void Update () {
}
}
Dome脚本
using UnityEngine;
using System.Collections;
public class Dome : MonoBehaviour {
void Start () {
//GameObject的方法GetComponent能够获取当前游戏对象身上制定的类型的组件
Test t =GetComponent<Test> ();
t.age = 24;
t.name="HAHA";
t.Log ();
}
void Update () {
}
}