GameObject数组的随机颜色

时间:2017-04-12 20:09:01

标签: c# unity3d random colors

统一制作游戏,我正在使用此代码。我不知道出了什么问题,为什么精灵在实例化时不会改变颜色。你能帮帮忙,所以我不会失去理智吗? :D(我也会随机生成GameObjects)

int randomIndex = UnityEngine.Random.Range(0, arrows.Length);
    GameObject prefab = arrows[randomIndex];
    GameObject clone = Instantiate(prefab, new Vector3(0.02F, 2.18F, -1), Quaternion.identity);

    //change colors
    colors[0] = new Color (250, 250, 250);
    colors[1] = new Color (144, 249, 242);
    colors[2] = new Color (20, 173, 163);
    colors[3] = new Color (21, 129, 168);
    colors[4] = new Color (5, 95, 127);
    colors[5] = new Color (58, 125, 196);
    int colorRandomIndex = UnityEngine.Random.Range(0, colors.Length);
    SpriteRenderer renderer = clone.GetComponent<SpriteRenderer>();
    renderer.color = colors[colorRandomIndex]; 
    myObjects.Add(clone);

2 个答案:

答案 0 :(得分:0)

也许试试renderer.material.color = colors[colorRandomIndex];?自从我使用Unity以来已经有一段时间......

答案 1 :(得分:0)

你能尝试使用

吗?
Check for working C compiler using: Visual Studio 10
Check for working C compiler using: Visual Studio 10 -- broken
**CMake Error at C:/ITK/CMake/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:52 (MESSAGE):
  The C compiler "cl" is not able to compile a simple test program.
  It fails with the following output:
   Change Dir: C:/ITK/Build/CMakeFiles/CMakeTmp

  Run Build Command:C:\PROGRA~2\MICROS~2.0\Common7\IDE\devenv.com
  CMAKE_TRY_COMPILE.sln /build Debug /project cmTryCompileExec1875248370

  Microsoft (R) Visual Studio Version 10.0.30319.1.
  Copyright (C) Microsoft Corp.  All rights reserved.
  1>------ Build started: Project: cmTryCompileExec1875248370, Configuration:
  Debug Win32 ------
  1> Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01
  for 80x86
  1> Copyright (C) Microsoft Corporation.  All rights reserved.
  1> 
  1> cl /c /Zi /W3 /WX- /Od /Ob0 /Oy- /D WIN32 /D _WINDOWS /D _DEBUG /D
  "CMAKE_INTDIR=\"Debug\"" /D _MBCS /Gm- /RTC1 /MDd /GS /fp:precise
  /Zc:wchar_t /Zc:forScope /Fo"cmTryCompileExec1875248370.dir\Debug\\"
  /Fd"C:/ITK/Build/CMakeFiles/CMakeTmp/Debug/cmTryCompileExec1875248370.pdb"
  /Gd /TC /analyze- /errorReport:prompt
  C:\ITK\Build\CMakeFiles\CMakeTmp\testCCompiler.c /Zm1000 
  1> 
  1> testCCompiler.c
  1>LINK : fatal error LNK1123: failure during conversion to COFF: file
  invalid or corrupt
  ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
  ==========


  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:7 (project)**


Configuring incomplete, errors occurred!

好的,错误就是你创建颜色的方式,Color只接受0到1之间的浮点值。你需要使用Color32

int colorRandomIndex = UnityEngine.Random.Range(0, colors.Length);

clone.GetComponent<SpriteRenderer>().material.color = colors[colorRandomIndex];

不要忘记也将数组更改为colors[0] = new Color32 (250, 250, 250, 255); colors[1] = new Color32 (144, 249, 242, 255); colors[2] = new Color32 (20, 173, 163, 255); colors[3] = new Color32 (21, 129, 168, 255); colors[4] = new Color32 (5, 95, 127, 255); colors[5] = new Color32 (58, 125, 196, 255);