unity常用的引用赋值一个GameObject的三种方法
方法 一个 常用 Unity 三种 引用 赋值
2023-09-27 14:25:49 时间
1, 直接拖对象赋值。
public GameObject cube;
2, Find函数
public GameObject cube;
public void ChangeColor2Red()
{
print ("Change cube color to red");
cube = GameObject.Find ("Cube");
cube.GetComponent<MeshRenderer> ().material.color = Color.red;
}
cube = GameObject.FindWithTag ("Cube");//这儿是Tag值
cube = GameObject.FindGameObjectWithTag ("Cube");
参照之前的博文find函数找物体的方法
3, 不一样的拖动赋值。
实例化moveCube
ChangeColor.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeColor : MonoBehaviour {
public Move moveCube;
// Use this for initialization
void Start () {
moveCube.ChangeColor2Red ();
}
// Update is called once per frame
void Update () {
}
}
Move.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour {
private GameObject cube;
public void ChangeColor2Red()
{
print ("Change cube color to red");
cube = GameObject.Find ("Cube");
cube.GetComponent<MeshRenderer> ().material.color = Color.red;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
相关文章
- 一个正确的编程学习方法
- 用Python批量把文件复制到另一个文件夹的实现方法
- 工作总结 @Html 辅助方法 为 生成的 标签设置元素属性 htmlAttributes 一个对象,其中包含要为该元素设置的 HTML 特性。
- MySQL 性能调优的10个方法
- vs2010里在【/* 和 */】的注释里,按回车自动多了一个【*】号的解决方法!
- 基于焦散线方法的自加速光束设计
- 第四章 方法定义
- OpenCV_Mat类对象常用属性值的获取方法
- 微信小程序 - 返回前一个页面时,执行前一个页面的函数方(wx.navigateBack 返回后,执行上一页的某个函数方法刷新数据)回前一个页面时,执行前一个页面的函数方法。支持改变 data 数据。
- Linux 下查看某一个程序所使用的内存方法
- 运维前线:一线运维专家的运维方法、技巧与实践导读
- C#中UDP通信过程中出现:远程主机强迫关闭了一个现有的连接0x80004005】的解决方法
- 在C++中可以用3种方法访问一个字符串
- java之方法的重写
- 一个Java方法能使用多少个参数?
- table下tbody滚动条与thead对齐的方法且每一列可以不均等
- 设备管理器,其他设备,PCI数据捕获和信号处理控制器出现感很多未知设备感叹号,通用解决方法,以华为matebook为例
- Hystrix的一个坑,queue中的run方法没有被执行?
- 封装一个生成uuid的方法
- SpringBoot报错处理:Exception processing template "XXX": Error resolving template [XXX]、expression expected找不到符号的错误、illegal escape character in String literal、Dao层方法不能重载
- weblogic 异常常见处理方法
- TotoiseSVN的基本使用方法