Unity3D 获得GameObject组件的方法有几种,这里进行说明一下:

组件:

要获得这些组件,进行操作的话,绑定一个Movescipt 的C#组件,里面的获取方法为

    void Update () {

        Debug.LogError("sprite=" + gameObject.GetComponent<SpriteRenderer>().sprite);
Debug.LogError("sortingOrder=" + gameObject.GetComponent<SpriteRenderer>().sortingOrder);
Debug.LogError("color=" + gameObject.GetComponent<SpriteRenderer>().color); Debug.LogError("position=" + gameObject.GetComponent<Transform>().position);
Debug.LogError("rotation=" + gameObject.GetComponent<Transform>().rotation);
Debug.LogError("localScale=" + gameObject.GetComponent<Transform>().localScale); Debug.LogError("position=" + gameObject.transform.position);
Debug.LogError("rotation=" + gameObject.transform.rotation);
Debug.LogError("localScale=" + gameObject.transform.localScale); }

这样通过获得组件GetComponent<>方法,能够获得一些需要的属性。

需要说明一下,

GetComponent<Transform>() 和 gameObject.transform 都能够获得组件的形态对象,只是写法不同罢了,推荐第一张写法,后面的方法估计以后也就是会废弃。

获得同一对象下面的其他组件也是同样的方法。
当要获得游戏对象下面的字对象的时候,用
 shootscript shoot= gameObject.GetComponentInChildren<shootscript>();

就可以得到子对象,也可以用集合的方式得到子对象的集合,进行操作,反之,可以从子对象得到父对象

  movescript ms = gameObject.GetComponentInParent<movescript>();

当然,能够拿到子对象或者父对象的组件了,也可以顺带得到该对象,对该对象进行处理了

        GameObject move =    gameObject.GetComponentInParent<movescript>().gameObject;
Debug.LogError("prant -----move = " + move.transform.position);

当两个游戏对象是平级的时候,如果要获得另一个游戏对象的属性

Find 是获得的对象名字

        Test2 test2 = GameObject.Find("1wwww").GetComponent<Test2>();
test2.SayTest2();
FindGameObjectWithTag 是获得的游戏对象的Tag,这个是可以自己去定义的,同时,当然可以进行获得到一个集合了
Test2 test2 = GameObject.FindGameObjectWithTag("Player").GetComponent<Test2>();
test2.SayTest2();

也是同样的获得Tag

 Test2 test2 = GameObject.FindWithTag("").GetComponent<Test2>();
test2.SayTest2();

Unity3D 获得GameObject组件的方法的更多相关文章

  1. React-Native 组件开发方法

    前言 React Native的开发思路是通过组合各种组件来组织整个App,在大部分情况下通过组合View.Image等几个基础的组件,可以非常方便的实现各种复杂的跨平台组件,不过在需要原生功能支持. ...

  2. WPF中实例化Com组件,调用组件的方法时报System.Windows.Forms.AxHost+InvalidActiveXStateException的异常

    WPF中实例化Com组件,调用组件的方法时报System.Windows.Forms.AxHost+InvalidActiveXStateException的异常 在wpf中封装Com组件时,调用组件 ...

  3. 查看 activex 组件的方法

    查看 activex 组件的方法 可以使用的工具COMRaider 直接安装 并选择对应的类型即可查看相关的信息,比OLE/COM Object Viewer 简洁方便. 具体的操作如下: 随意选择一 ...

  4. 解决 window server2008 r2 没有注册Ofiice组件的方法

    解决 window server2008  r2 没有注册Ofiice组件的方法   .NET下在用Microsoft.Office.Interop.Excel及word 操作Excel和Word时, ...

  5. VC中调用COM组件的方法(转载)

    原文参考:http://hi.baidu.com/mingyueye/item/53ebecd44da76917d80e4449 总结一下在VC中调用COM组件的方法 准备及条件: COM服务器为进程 ...

  6. Unity3D与iOS消息交互方法(1)--iOS接收Unity3D发出的消息

    跨平台这种事情不管多NB, 总要有些与原生系统交互的方法, 比如  Unity3D与iOS消息交互方法. 一: 建立一个空的Unity工程. File -->  New Project 二: 编 ...

  7. vc中调用Com组件的方法详解

    vc中调用Com组件的方法详解 转载自:网络,来源未知,如有知晓者请告知我.需求:1.创建myCom.dll,该COM只有一个组件,两个接口:   IGetRes--方法Hello(),   IGet ...

  8. 在javascript中使用com组件的方法

    转载自:  http://dhailin.blog.163.com/blog/static/230738322011128102043880/ 首先创建一个COM组件,插入一个双接口Itest,在此接 ...

  9. 在JS中使用COM组件的方法

    首先创建一个COM组件,插入一个双接口Itest,在此接口上实现以下三个方法: STDMETHODIMP Ctest::test(void) //无输入输出参数 { // TODO: 在此添加实现代码 ...

随机推荐

  1. C语言实现定积分求解方法

    求定积分的方法有很多种,下面是我总结的几种比较常用的方法. #include <stdio.h> #include <stdlib.h> #include <math.h ...

  2. 利用if else判断几点是什么时间段

    static void Main(string[] args)        {            while (true)            {                int a;  ...

  3. ASP.Net4.0中新增23项功能

    这篇文章介绍Visual Studio 2010 (ASP.Net 4.0)的新功能. 1.代码片段(Code Snippets): 代码段是预先开发的代码模板,可以节省我们对有关语法思考的时间.在V ...

  4. BJOI2006狼抓兔子

    1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 9967  Solved: 2267[Submit][S ...

  5. team geek

    1. 转载自http://book.douban.com/review/6007037/,版权归丸子(^.^)v所有. New Google employees (we call “Nooglers” ...

  6. Service Oriented Architecture and WCF 【转】

    http://www.codeproject.com/Articles/515253/Service-Oriented-Architecture-and-WCF Introduction This a ...

  7. (转)Python:self

    原文:http://www.douban.com/group/topic/19376685/ 这是对前面一个php程序员问python方法为什么要手写一个self的回答,当时那个帖非常的热闹,但是下面 ...

  8. Java Servlet 工作原理问答

    导读 本文来自stackoverflow的问答,讨论了Java Servlet的工作机制,如何进行实例化.共享变量和多线程处理. 问题:Servlet是如何工作的?Servlet 如何实例化.共享变量 ...

  9. jmeter http get 登录

      登录页url:http://www.ablesky.com/login.do?fromurl=http%3A%2F%2Fwww.ablesky.com%2Findex.do 登录参数用firebu ...

  10. array_column php 函数

    今天想从二维数组中取某个列成一维数组 结果发现 array_column需要php5.5支持才行 然后自己写了一个仿造版本的array_column 两种思路 1.直接遍历 取值给追加数组 返回 2. ...