Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d Unity Spriterender在脚本中设置材质_Unity3d_Components_Sprite - Fatal编程技术网

Unity3d Unity Spriterender在脚本中设置材质

Unity3d Unity Spriterender在脚本中设置材质,unity3d,components,sprite,Unity3d,Components,Sprite,我只想在脚本中设置精灵渲染器的材质。我该怎么做? 因此,我想在代码中设置精灵渲染器的材质,但它不起作用,我也不知道为什么。通过拖放精灵渲染器和要切换到的材质,可以在检查器中设置对象/新材质 编辑: 公共游戏对象; 公共材料新材料; //... object.GetComponent().material=newMaterial; 但在精灵渲染器中,有一个包含材质的字段 public GameObject object; public Material newMaterial; //... obj

我只想在脚本中设置精灵渲染器的材质。我该怎么做?
因此,我想在代码中设置精灵渲染器的材质,但它不起作用,我也不知道为什么。

通过拖放精灵渲染器和要切换到的材质,可以在检查器中设置对象/新材质

编辑:

公共游戏对象;
公共材料新材料;
//...
object.GetComponent().material=newMaterial;

但在精灵渲染器中,有一个包含材质的字段
public GameObject object;
public Material newMaterial;
//...
object.GetComponent<SpriteRenderer>().material = newMaterial;