N
N
Nekit Medvedev2021-01-20 15:16:22
Unity
Nekit Medvedev, 2021-01-20 15:16:22

How to refer to the spriteRender of an object in an array?

Hello comrades!!!!
There is this code:

public GameObject block;
    //   public float startPosX;
    //   public float startPosY;
    public int countX;
    public int countY;
    public float outX;
    public float outY;
    public string objName = "Block_";
    private int id;
    public GameObject[,] grid;
       
    

    // Start is called before the first frame update
    void Start()
    {
        float startPosX = block.transform.position.x;
        float startPosY = block.transform.position.y;

        id = 0;
        float posXreset = startPosX;
        grid = new GameObject[countX, countY];
        for (int y = 0; y < countY; y++)
        {
            startPosY -= outY;
            for (int x = 0; x < countX; x++)
            {
                id++;
                startPosX += outX;
                grid[x, y] = Instantiate(block, new Vector2(startPosX, startPosY), Quaternion.identity) as GameObject;
                grid[x, y].name = objName + id;
                grid[x, y].transform.parent = transform;//работает
                grid[x, y].spriteRenderer.material = null;//не работает
            }
            startPosX = posXreset;
        }
    }

It generates a grid from block objects and stores it in an array to refer to.
In this script, there is access to the transform component, but not to the spriteRenderer component. Why is this happening and how to get access?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2021-01-20
@NIKROTOS

GetComponent

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question