jeudi 21 juillet 2016

Unity C# Instantiate with GetComponent in Stack?

I hope someone can help me here...

My Idea was to create a Color lerp in my game. I have a stack, and each new spawned object should spawn with a different color. Sadly I don't know how to apply this in the stack...

My Code:

  private void ColorMesh(Mesh mesh)
    {
        Vector3[] vertices = mesh.vertices;
        Color32[] colors = new Color32[vertices.Length];

        float f = Mathf.Sin(Time.deltaTime * 0.25f);

        for (int i = 0; i < vertices.Length; i++)
            colors[i] = Lerp4(gameColors[0], gameColors[1], gameColors[2], gameColors[3], f);
        mesh.colors32 = colors;
    }

    private Color32 Lerp4(Color32 a, Color32 b, Color32 c, Color32 d, float t)
    {
        if (t < 0.33f)
            return Color.Lerp(a, b, t / 0.33f);
        else if (t < 0.66f)
            return Color.Lerp(b, c, (t - 0.33f) / 0.33f);
        else
            return Color.Lerp(c, d, (t - 0.66f) / 0.66f);
    }

    public void CreateTiles(int amount)
    {

        for (int i = 0; i < amount; i++)
        {
            //Creates a tile and adds it to the stack
            tileEmpty.Push(Instantiate(tilePrefabs[0]));
            tileEmpty.Peek().name = "TileEmpty";
            tileEmpty.Peek().SetActive(false);
        }

How do I now apply the color change? I know it has to be after tileEmpty.Push... in my for loop, but how? Everything I tried with GetComponent().mesh; wasn't working... All my spawned objects have the same color.

Thank you very much for your time and your help!

Best, Chris




Aucun commentaire:

Enregistrer un commentaire